Spring IOC

1:项目添加spring的支持

a.普通java项目配置如下

1 <dependency>
2       <groupId>org.springframework</groupId>
3       <artifactId>spring-context</artifactId>
4       <version>3.2.9.RELEASE</version>
5   </dependency>

 

b.web项目配置如下

1 <dependency>
2       <groupId>org.springframework</groupId>
3       <artifactId>spring-web</artifactId>
4       <version>3.2.9.RELEASE</version>
5   </dependency>

 同时在web.xml中配置spring的监听器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6 
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>
10             <!-- 此路径为Spring配置文件applicationContext.xml路径 -->
11             classpath:/applicationContext.xml
12         </param-value>
13     </context-param>
14     
15     <listener>
16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17     </listener>
18 </web-app>

 

2:实体配置

Address.java

1 public class Address {
2     private String address;
3 }

Person.java

 1 public class Person {
 2     private String name;
 3     private Address address;
 4     private List<String> friends;
 5     private Set<String> family;
 6     private Map<String,String> relation;
 7     private Properties identity;
 8     public void init() {
 9         System.out.println("init");
10     }
11     
12     public void destroy() {
13         System.out.println("destroy");
14     }
15 }      

 

 3:配置applicationContext.xml,具体配置如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9     
10     <bean id="address" class="test.Address">
11         <!-- 向构造函数注入类对象 <constructor-arg index = "0" ref = "address"/> -->
12         <constructor-arg index = "0" value = "zhejiang"/>
13     </bean>
14 
15     <bean id="person" class="test.Person" init-method="init" destroy-method="destroy" scope="prototype">
16         <!-- 注入普通属性 -->
17         <property name="name" value="zanglitao"/>
18         
19         <!-- 注入类对象 -->
20         <property name="address" ref="address"/>
21         
22         <!-- 注入List -->
23         <property name="friends">
24             <list>
25                 <!-- 向List中注入类对象 <ref bean=""/> -->
26                 <value>yao</value>
27                 <value>ye</value>
28                 <value>pin</value>
29                 <value>cao</value>
30             </list>
31         </property>
32         
33         <!-- 注入Set -->
34         <property name="family">
35             <set>
36                 <!-- 向Set中注入类对象 <ref bean=""/> -->
37                 <value>father</value>
38                 <value>mather</value>
39             </set>
40         </property>
41         
42         <!-- 注入Map -->
43         <property name="relation">
44             <map>
45                 <!-- map的key和value引用其他类对象 <entry value-ref="" value-ref=""/> -->
46                 <entry key="father" value="zang"/>
47                 <entry key="mather" value="ma"/>
48             </map>
49         </property>
50         
51         <!-- 注入Properties -->
52         <property name="identity">
53             <props>
54                 <prop key="gender">male</prop>
55                 <prop key="hobby">readding</prop>
56             </props>
57         </property>
58     </bean>
59 </beans>

 其中bean的scope属性默认为singleton,web项目中可以设置为request和session

 

4:测试程序

1 public class SpringTest {
2     public static void main(String[] args) {
3         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
4         Person person = (Person)ac.getBean("person");
5     }
6 }

 获得的Person对象所有属性都已经注入

 

5:Annotation配置

a.配置applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9     
10     <!-- 扫描的包 -->
11     <context:component-scan base-package="test"/>
12 </beans>

 

b.配置实体

Address.java

 1 @Scope("prototype")
 2 @Component("address")
 3 public class Address {
 4     @Value("address")
 5     private String address;
 6 
 7     public String getAddress() {
 8         return address;
 9     }
10 }

 Person.java

 1 @Scope("prototype")
 2 @Component
 3 public class Person {
 4     
 5     @Value("zanglitao1")
 6     private String name;
 7     
 8     /**
 9      * @Autowired 默认byType
10      * 当定义了多个相同类型的bean时,在@Autowired下使用@Qualifier("address")可以具体指定需要的bean
11      * @Autowired(required = false) 未找到装配的类也不会报错
12      */
13     @Autowired
14     @Qualifier("address")
15     private Address address1;
16     
17     
18     /**
19      * @Resource 有name和type两个属性
20      * @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。
21      * 所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。
22      * 如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略
23      */
24     @Resource(name="address")
25     private Address address2;
26     
27     //实体对象创建后调用
28     @PostConstruct
29     public void init() {
30         System.out.println("init");
31     }
32     
33     //实体对象摧毁前调用
34     @PreDestroy
35     public void destroy() {
36         System.out.println("destroy");
37     }
38 }

 

Spring IOC,布布扣,bubuko.com

Spring IOC

上一篇:JAVA中BufferedReader设置编码的必要性


下一篇:git push throws error: RPC failed; result=22, HTTP code = 411的解决办法