@EggGump
2018-03-05T07:53:41.000000Z
字数 10371
阅读 560
未分类
创建一个javaproject learnSpring
创建一个lib文件夹,commons-logging.jar和spring.jar放入里面,并add to build path
在learnSpring的properties中,java Build Path/Libraries/add Library/Junit/Junit4
创建包com.neu.hello
创建类com.neu.hello.HelloWorld.java
package com.neu.hello;public class HelloWorld {public void hello() {System.out.println("Hello") ;}}
创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id = "helloWorld" class = "com.neu.hello.HelloWorld"></bean></beans>
创建包com.neu.test
创建类com.neu.test.HelloTest.java
package com.neu.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.neu.hello.HelloWorld;public class HelloTest {@Testpublic void testHello() {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml") ;HelloWorld hello = (HelloWorld)context.getBean("helloWorld") ;hello.hello();}}
右击 run as / JUnit Test
别名:xml文件里加:
<alias name="helloWorld" alias="shit"/> //name要和类的id一致
这样,便可利用 context.getBean("shit")来获得一个新象
spring默认构造对象是用无参的构造函数进行。
静态工厂创建对象
先创建类
package com.neu.hello;public class HelloWorldFactory {public static HelloWorld getInstance() {return new HelloWorld() ;}}
再在xml中添加如下内容
<bean id = "helloWorld2" class = "com.neu.hello.HelloWorldFactory" factory-method="getInstance"></bean>
最后在客户端这样写就可以了:
HelloWorld hello = (HelloWorld)context.getBean("helloWorld2") ;
实例工厂创建对象
xml文件添加:
<!-- 实例工厂获得类的方法不能是静态的,否则出错--><bean id = "helloWorldFactory" class = "com.neu.hello.HelloWorldFactory"></bean><bean id = "helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance2"></bean>
工厂类里:
package com.neu.hello;public class HelloWorldFactory {public HelloWorld getInstance2() {return new HelloWorld() ;}}
测试类:
HelloWorld hello = (HelloWorld)context.getBean("helloWorld3");hello.hello();
- 第一种默认情况下,spring容器产生的对象是单例的,也就是说,context.getBean("xxx")执行多次,也只会产生一个对象。
<bean id = "helloWorld" class = "com.neu.hello.HelloWorld" scope ="prototype"></bean>
多添加一个scope便可让helloWorld产生多例对象,此时,在getBean时会创建对象。默认只会在spring容器初始化时创建对象,当lazy-init="true"时,便会在调用getBean(x)时产生对象,当有scope时,lazy-init失效
- init-method和destroy-method
xml文件中添加参数
<bean id = "helloWorld" class = "com.neu.hello.HelloWorld" scope="prototype"init-method="init" destroy-method="destroy"></bean>
HelloWorld文件中添加方法
package com.neu.hello;public class HelloWorld {public HelloWorld() {System.out.println("new instance") ;}public void init() {System.out.println("init") ;}public void destroy() {System.out.println("destroy");}public void hello() {System.out.println("Hello") ;}}
spring在创建对象时,会调用init方法,在容器销毁时会调用destroy方法测试代码:这里需要强制转换才能关闭容器。如果有scope=prototype,那么不负责销毁。
public void test() {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml") ;HelloWorld hello = (HelloWorld)context.getBean("helloWorld") ;hello.hello();ClassPathXmlApplicationContext applicationContext =(ClassPathXmlApplicationContext)context ;applicationContext.close();}
类文件Person.java 这里的set 和 get方法一定要按格式写,否则后面xml文件写时会出错
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Person {private Long pid;private String name;private Student student;private List lists;private Set sets;private Map map;private Object[] objects;private Properties properties;public Person(){System.out.println("person");}public Long getPid() {return pid;}public void setPid(Long pid) {this.pid = pid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public List getLists() {return lists;}public void setLists(List lists) {this.lists = lists;}public Set getSets() {return sets;}public void setSets(Set sets) {this.sets = sets;}public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}public Object[] getObjects() {return objects;}public void setObjects(Object[] objects) {this.objects = objects;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "Person [pid=" + pid + ", name=" + name + ", student=" + student+ ", lists=" + lists + ", sets=" + sets + ", map=" + map+ ", objects=" + Arrays.toString(objects) + ", properties="+ properties + "]";}}
文件Student.java
public class Student {public Student(){System.out.println("student");}public void say(){System.out.println("student");}}
xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="person" class="com.itheima11.spring.di.xml.setter.Person"><!--property描述的就是bean中的属性name属性就是描述属性的名称value就是值 如果是基本属性(String),就用value赋值ref 如果是引用类型,用ref赋值--><property name="pid" value="1"></property><property name="name" value="王二麻子"></property><property name="student" ref="student"></property><property name="lists"><list><value>list1</value><value>list2</value><!--list中存放一个student对象--><ref bean="student"/></list></property><property name="objects"><list><value>obj1</value><ref bean="student"/></list></property><property name="sets"><set><value>set1</value><ref bean="student"/></set></property><property name="map"><map><entry key="m1"><value>m1</value></entry><entry key="m2"><ref bean="student"/></entry></map></property><property name="properties"><props><prop key="p1">p1</prop><prop key="p2">p2</prop></props></property></bean><bean id="student" class="com.itheima11.spring.di.xml.setter.Student"scope="prototype"></bean></beans>
test.java
public class DIXMLSetterTest {/*** 1、启动spring容器* 2、给person创建对象* 3、给student创建对象* 4、调用person的各个属性的setter方法赋值* 5、context.getBean* 6、对象调用方法*/@Testpublic void testDI_XML_Setter_Default(){ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");System.out.println(person.toString());}@Testpublic void testDI_XML_Setter_Person_Default_Student_Lazyinit_TRUE(){ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");System.out.println(person.toString());}/*** 1、启动spring容器* 2、创建Student对象* 3、context.getBean* 4、创建person对象* 5、调用setter方法赋值* 6、对象调用方法*/@Testpublic void testDI_XML_Setter_Person_Lazyinit_TRUE_Student_Lazyinit_Default(){ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");System.out.println(person.toString());}/*** 1、启动spring容器* 2、创建Student对象* 3、context.getBean* 4、创建person对象* 5、调用setter方法赋值* 6、对象调用方法*/@Testpublic void testDI_XML_Setter_Person_Scope_Prototype_Student_Scope_Default(){ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");System.out.println(person.toString());}/*** 1、启动spring容器* 2、创建person对象* 3、创建student对象 student的scope为"prototype",但是创建对象在spring容器启动的时候* 因为Person中的很多属性都依赖于student,而这些属性的赋值发生在spring容器启动的时候* 4、调用setter方法赋值* 5、 context.getBean* 6、对象调用方法*/@Testpublic void testDI_XML_Setter_Person_Scope_Default_Student_Scope_Prototype(){ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");System.out.println(person.toString());}}
所有的scope和lazy-init都默认情况下,如果Person有init-method方法,那么选执行完所有的set方法进行装配,再调用init方法。
Person.java
package com.neu.hello;import java.util.* ;public class Person {private Long pid ;private String name ;private Student student ;public Person(Long pid, String name, Student student) {super();this.pid = pid;this.name = name;this.student = student ;}@Overridepublic String toString() {return "Person [pid=" + pid + ", name=" + name + ", student=" + student + "]";}}
Student.java ,Test.java同上
xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id = "person" class = "com.neu.hello.Person"><constructor-arg index="0" value="1"></constructor-arg><constructor-arg index = "1" value="shit"></constructor-arg><constructor-arg index = "2" ref="student"></constructor-arg></bean><bean id = "student" class = "com.neu.hello.Student"></bean></beans>
注意,引用类型的使用ref
今天学了一个新玩意儿
先创建两个注解 annotation
ClassInfo.java
package com.neu.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)//该注解能作用于class等上@Retention(RetentionPolicy.RUNTIME)public @interface ClassInfo {String name() default "" ;}
MethodInfo.java
package com.neu.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD) //该注解能作用于方法@Retention(RetentionPolicy.RUNTIME)public @interface MethodInfo {String name() default "" ;}
再创建一个类,将刚才创建的注解用上
Cloud.java
package com.neu.annotation;@ClassInfo(name = "11")public class Cloud {@MethodInfo(name = "learn java")public void java() {}}
最后再创建一个文件,通过反射将刚才的注解显示出来
AnnotationParse.java
package com.neu.annotation;import java.lang.reflect.Method;import org.junit.Test;public class AnnotationParse {public static void parse() {Class class1 = Cloud.class ;//判断该类上有没有ClassInfo注解if(class1.isAnnotationPresent(ClassInfo.class)) {ClassInfo classInfo = (ClassInfo)class1.getAnnotation(ClassInfo.class) ;System.out.println(classInfo.name());}Method[] methods = class1.getMethods() ;for(Method method : methods) {//判断当前正在遍历的方法上是否有注解if(method.isAnnotationPresent(MethodInfo.class)) {MethodInfo methodInfo = method.getAnnotation(MethodInfo.class) ;System.out.println(methodInfo.name());}}}@Testpublic void Test() {AnnotationParse.parse();}}
还是spring注入的一种方式,所以要按前面所说的spring方式创建java工程,这里,xml的命名空间要做一些改变,改变如下:
xml文件与原来相比多了几行
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">