Spring简介
1, 只用IOC
spring.jar , jarkata-commons/commons-loggin.jar
2,IOC容器
实例化具体bean
动态装配
3,AOP支持
安全检查
管理transaction
Spring配置
1,注入类型
- setter(重要)
- 构造方法(可以忘记)
- 接口注入(可以忘记)
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"><!-- new UserDAOImpl()--> </bean> <bean id="userService" class="com.bjsxt.service.UserService"> <!-- <property name="userDAO" ref="u" /> setter注入 ,调用setxxx方法 --> <constructor-arg> <!-- 构造方法注入,需要在UserService里面写构造方法,相当于调用了构造方法--> <ref bean="u"/> <!-- 参考了另外一个已经new 的bean--> </constructor-arg> </bean>
public class UserService { private UserDAO userDAO; public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public UserService(UserDAO userDAO) {//用于构造方法注入 super(); this.userDAO = userDAO; } }
2,id vs. name
name可以用特殊字符
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService"> <!-- <property name="userDAO" ref="u" /> --> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean>
3,简单属性的注入
bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="daoId" value="8"></property> <property name="daoStatus" value="good"></property> </bean>
public class UserDAOImpl implements UserDAO { private int daoId; private String daoStatus; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public String getDaoStatus() { return daoStatus; } public void setDaoStatus(String daoStatus) { this.daoStatus = daoStatus; } public void save(User user) { System.out.println("user saved!"); } @Override public String toString() { return this.daoId + ":" + this.daoStatus; } }
4,bean 中的scope属性
singleton:只有一个
prototype:每一个都不同
5,集合注入
很少用,不重要!
public class UserDAOImpl implements UserDAO { /* (non-Javadoc) * @see com.bjsxt.dao.UserDAO#save(com.bjsxt.model.User) */ private Set<String> sets; private List<String> lists; private Map<String , String> maps; public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public UserDAOImpl() { } public void save(User user) { System.out.println("user saved!"); } @Override public String toString() { return "sets size:" + sets.size() + "| lists size:" + lists.size() + "| maps size:" + maps.size() ; } }
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> <entry key="4" value="4"></entry> </map> </property> </bean> <bean id="userService" class="com.bjsxt.service.UserService"> <!-- <property name="userDAO"> <ref bean="userDAO"/> </property> --> <constructor-arg> <ref bean="userDAO"/> </constructor-arg> </bean>
6,自动装配
- byName
- byType
- 如果所有的bean都用同一种,可以使用beans的属性:default-autowire
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="daoId" value="1"></property> </bean> <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">
<!-- 未指定property, 若是byname则自动匹配name等于userDAO的bean,
若是bytype则匹配类型为userDAO的bean-->
</bean>
public class UserDAOImpl implements UserDAO { private int daoId; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public void save(User user) { System.out.println("user saved!"); } @Override public String toString() { return "daoId=" + daoId; } }
7,生命周期
- lazy-init (不重要) 容器初始化时(ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml")),不初始化,只有用到时才初始化
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
- init-method destroy-methd 不要和prototype一起用(了解)
<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="singleton"> <!-- <property name="userDAO" ref="u" /> --> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean>
public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public UserService(UserDAO userDAO) { super(); this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
8,Annotation第一步:
修改xml文件,参考文档<context:annotation-config />
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="u2" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService" > </bean> </beans>
9,@Autowired(自动装配)
- 默认按类型by type
- 如果想用byName,使用@Qulifier
- 写在private field(第三种注入形式)(不建议,破坏封装)
- 如果写在set上,@qualifier需要写在参数上
public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Autowired public void setUserDAO(@Qualifier("u") UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
10,@Resource(重要)
- 加入:j2ee/common-annotations.jar
- 默认按名称,名称找不到,按类型 @Resource
- 可以指定特定名称 @Resource(name="u2")
- 推荐使用
- 不足:如果没有源码,就无法运用annotation,只能使用xml
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="u2" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService" > </bean> </beans>
public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u2") //@Resource public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
11,@Component @Service @Controller @Repository 在spring2.5.6中 这四个目前没有区别
- 初始化的名字默认为类名首字母小写
- 可以指定初始化bean的名字 @Component("u")
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> </beans>
@Component("u") //u=new UserDAO() public class UserDAOImpl implements UserDAO { public void save(User user) { System.out.println("user saved!"); } }
@Component("userService") public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") //把name=u的UserDAO注入 public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
12,@Scope
13,@PostConstruct = init-method; @PreDestroy = destroy-method;
@Scope("singleton") @Component("userService") public class UserService { private UserDAO userDAO; @PostConstruct public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } @PreDestroy public void destroy() { System.out.println("destroy"); } }