1 Spring是一个解决企业应用的一个web容器,它的核心技术是IOC和AOP.
2 IoC 控制反转:
之前创建对象,需要手动去new,自己决定如何创建对象。在spring容器中,通过配置bean,来获取对象,让spring容器代替我们管理对象,这就是控制反转。
package com.sp.temp; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext sc=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao ud=(UserDao) sc.getBean("userDao"); System.out.println(ud.getAge()+":"+ud.getStr()); List<String> list=ud.getList(); for(String str:list){ System.out.println("list:"+str); } Set<String> set=ud.getSet(); for(String s:set){ System.out.println("set:"+s); } Map map=ud.getMap(); Set<String> keys=map.keySet(); for(String s:keys){ System.out.println(s+":"+(String)map.get(s)); } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="<a target=_blank href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>" xmlns:xsi="<a target=_blank href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>" xmlns:context="<a target=_blank href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a>" xmlns:tx="<a target=_blank href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a>" xmlns:aop="<a target=_blank href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a>" xsi:schemaLocation="<a target=_blank href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a> <a target=_blank href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a> <a target=_blank href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a> <a target=_blank href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</a>"> <bean name="userDao" class="com.sp.temp.UserDao"> <property name="age" value="10"></property> <property name="str" value="test"></property> <property name="list"> <list> <value>张三</value> <value>李四</value> <value>王五</value> </list> </property> <property name="set"> <set> <value>张三</value> <value>张三1</value> </set> </property> <property name="map"> <map> <entry> <key><value>zg</value></key> <value>中國</value> </entry> <entry> <key><value>mg</value></key> <value>美国</value> </entry> </map> </property> </bean> <bean name="userService" class="com.sp.temp.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
ApplicationContext继承于BeanFactory。
注入需要有属性声明和set方法。
3 用注解的方式完成注入:
(1) 在applicationContext.xml里的xsi:schemaLocation加入:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
(2)被注入的属性值的set方法上加 @Autowired
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean name="userDao" class="com.sp.temp.UserDao"> <property name="age" value="10"></property> <property name="str" value="test"></property> <property name="list"> <list> <value>张三</value> <value>李四</value> <value>王五</value> </list> </property> <property name="set"> <set> <value>张三</value> <value>张三1</value> </set> </property> <property name="map"> <map> <entry> <key><value>zg</value></key> <value>中國</value> </entry> <entry> <key><value>mg</value></key> <value>美国</value> </entry> </map> </property> </bean> <bean name="userService" class="com.sp.temp.UserService"> </bean> </beans>
在userService的bean中并没有配置属性userDao;
package com.sp.temp; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; public class UserService { private UserDao userDao; private String str; public UserDao getUserDao() { return userDao; } @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
userDao的set方法上面加了注解 @Autowired完成该属性的注入;
使用注解的方式可以不用对要注入的属性进行引入,但是必须声明过这种属性的bean。
原文地址:http://blog.csdn.net/liangwenmail/article/details/47450815