[Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
来源:互联网 发布:qq人工智能聊天机器人 编辑:程序博客网 时间:2024/06/05 05:38
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!");}@Overridepublic 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>UserService
package 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);}}UserServiceTest
package 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 {@Testpublic 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.java
package 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.java
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 { 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-autowire8, 生命周期 a), Spring_0900_IOC_Life_Cycle b), lazy-init (不重要) 我感觉不错。用到再实例化,不直接全初始化 c), init-method destroy-methd 不要和 prototype 一起用(了解)Spring 3.0 是 IOC 的容器。 IOC 差不多讲解完了。重点是 下面的AOP。
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Spring中Bean的简单属性注入
- Spring bean自动装配的属性值注入的个人见解
- Spring依赖注入 — 属性自动装配
- Spring依赖注入 — 属性自动装配
- spring 常用属性注入和自动装配
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- Spring入门之bean属性注入集合
- [Spring] 注入Bean属性
- Spring中bean标签的id与name属性scope属性
- Spring 注入集合、依赖检查、自动组件扫描、过滤组件自动扫描、自动装配属性
- Spring属性注入Bean的List属性
- Spring中Bean属性为集合的注入方式
- Spring的《XML显式装配bean》之通过属性property注入bean
- Spring简单属性注入和集合属性注入
- Spring注入集合属性
- Spring自动类型转换/集合属性注入
- spring自动装配Bean属性
- 栈:顺序栈和链式栈
- 关于C++ 虚基类
- delphi listview使用
- 通过show status 来优化 MySQL数据库
- Apache Shiro
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Android通讯录开发之解决快速搜索联系人线程同步问题
- iOS网络通信http之NSURLConnection
- CentOS 6.4 安装源(163的源)
- 到Autodesk 应用程序商店里一键下载大量插件;让你的插件传遍Autodesk产品的用户
- 深入浅出MFC 第8章 关于序列化(Serialize)的一些问题
- 转载和积累系列 - Lua 字符串库
- 利用Cobbler批量布署CentOS
- 黑马程序员_javaIO流_1