[Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
a), Spring_0400_IOC_Id_Name
b), name可以用特殊字符 beans.xml 中 <bean id/name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
4, 简单属性的注入
a), Spring_0500_IOC_SimpleProperty
b), <property name=… value=….>
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="8"></property>
<property name="daoStatus" value="good"></property>
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="userService" class="com.bjsxt.service.UserService">
<!-- <property name="userDAO" ref="u"/> -->
<constructor-arg>
<ref bean="userDAO"/>
</constructor-arg>
</bean>
<!-- more bean definitions go here -->
</beans> (因为是基础数据类型,其实我们自己写很少遇到这样的, 但Spring整合自己时,可以用到) 5, <bean 中的 scope 属性, 生命范围
a), Spring_0600_IOC_Bean_Scope
b), singleton 单例
c), proptotype 每次创建新的对象 <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">
6, 集合注入
a), Spring_0700_IOC_Collections (留下待定-现没有源码)
b), 很少用,不重要!参考程序
UserDAOImpl
package com.bjsxt.dao.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
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();
}
}
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="sets"> <!-- setSets -->
<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>
</beans>UserServicepackage com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
private UserDAO userDAO;
private UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}
/*public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
System.out.println("set");
this.userDAO = userDAO;
}*/
public void add(User user) {
System.out.println(this.userDAO);
this.userDAO.save(user);
}
}
UserServiceTestpackage com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.dao.UserDAO;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserDAO u1 = (UserDAO)ctx.getBean("userDAO");
System.out.println(u1);
}
}
7, 自动装配a), Spring_0800_IOC_AutoWire
b), byName
c), byType
d), 如果所有的bean都用同一种,可以使用 beans 的属性:default-autowire
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<!--
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>autowire="byName"
-->
<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType">
</bean>
</beans>package com.bjsxt.service;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) ctx.getBean("userService"); // new UserService();
System.out.println(service.getUserDAO());
}
}8, 生命周期a), Spring_0900_IOC_Life_Cycle
b), lazy-init (不重要) 我感觉不错。用到再实例化,不直接全初始化
c), init-method destroy-methd 不要和 prototype 一起用(了解)
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
</bean>
<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy">
<!-- <property name="userDAO" ref="u"/> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>UserService.javapackage com.bjsxt.service;
import com.bjsxt.dao.UserDAO; // 抽象出的数据库管理层
import com.bjsxt.dao.impl.UserDAOImpl;
import com.bjsxt.model.User;
public class UserService { // 业务逻辑, 还可以设计权限, 抽象出一层 : 用户服务层, 用户管理层
private UserDAO userDAO;
public void init() { /* service 初始化之前执行的方法 */
System.out.println("init");
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void add(User u) {
this.userDAO.save(u); // 添加用户会将用户添加到 DB 中。
}
public UserService(UserDAO userDAO) {
super();
this.userDAO = userDAO;
}
public void destroy() { /* service 结束,容器关闭后执行的方法 */
System.out.println("destroy");
}
}
UserServiceTest.javapackage com.bjsxt.service;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) ctx.getBean("userService"); // new UserService();
ctx.destroy();
}
}
Spring 3.0 是 IOC 的容器。 IOC 小总结笔记
3, id vs. name
a), Spring_0400_IOC_Id_Name
b), name可以用特殊字符 <bean id/name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
4, 简单属性的注入
a), Spring_0500_IOC_SimpleProperty
b), <property name=… value=….>
(因为是基础数据类型,其实我们自己写很少遇到这样的, 但Spring整合自己时,可以用到)
5, <bean 中的 scope 属性, 生命范围
a), Spring_0600_IOC_Bean_Scope
b), singleton 单例
c), proptotype 每次创建新的对象
6, 集合注入
a), Spring_0700_IOC_Collections
b), 很少用,不重要!参考程序
7, 自动装配
a), Spring_0800_IOC_AutoWire
b), byName
c), byType
d), 如果所有的bean都用同一种,可以使用 beans 的属性:default-autowire
8, 生命周期
a), Spring_0900_IOC_Life_Cycle
b), lazy-init (不重要) 我感觉不错。用到再实例化,不直接全初始化
c), init-method destroy-methd 不要和 prototype 一起用(了解)
3, id vs. name
a), Spring_0400_IOC_Id_Name
b), name可以用特殊字符 <bean id/name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
4, 简单属性的注入
a), Spring_0500_IOC_SimpleProperty
b), <property name=… value=….>
(因为是基础数据类型,其实我们自己写很少遇到这样的, 但Spring整合自己时,可以用到)
5, <bean 中的 scope 属性, 生命范围
a), Spring_0600_IOC_Bean_Scope
b), singleton 单例
c), proptotype 每次创建新的对象
6, 集合注入
a), Spring_0700_IOC_Collections
b), 很少用,不重要!参考程序
7, 自动装配
a), Spring_0800_IOC_AutoWire
b), byName
c), byType
d), 如果所有的bean都用同一种,可以使用 beans 的属性:default-autowire
8, 生命周期
a), Spring_0900_IOC_Life_Cycle
b), lazy-init (不重要) 我感觉不错。用到再实例化,不直接全初始化
c), init-method destroy-methd 不要和 prototype 一起用(了解)
Spring 3.0 是 IOC 的容器。 IOC 差不多讲解完了。重点是 下面的AOP。

浙公网安备 33010602011771号