1. 注入类型
a) Spring_0300_IOC_Injection_Type
b) setter(重要)
c) 构造方法(可以忘记)
d) 接口注入(可以忘记)
以下是setter 注入
- <?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 id="u" class="com.demo.dao.impl.UserDAOImpl"/>
- <bean id="userService" class="com.demo.service.UserService">
- <property name="userDAO" ref="u"/>
- </bean>
- </beans>
- package com.demo.service;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.demo.model.User;
- // Dependency Injection 依赖注入 依赖容器注入的对象 (灵活)
- // Inverse of Control 控制反转 原来具体的实现控制在自己手里,现在控制在容器手里
- public class UserServiceTest {
- @Test
- public void testAdd() throws Exception{
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- UserService service = (UserService)ctx.getBean("userService");
- User u = new User();
- u.setUsername("li");
- u.setPassword("ww");
- service.add(u);
- }
- }
构造方法注入
- public UserService(UserDAO userDAO) {
- super();
- this.userDAO = userDAO;
- }
- <constructor-arg>
- <ref bean="u"/>
- </constructor-arg>
1. id vs. name
a) Spring_0400_IOC_Id_Name
b) name可以用特殊字符
<bean name="u" class="com.demo.dao.impl.UserDAOImpl"/> 把 id 改成name 原因是可以加特殊字符 (不重要)
1. 简单属性的注入(不重要)
a) Spring_0500_IOC_SimpleProperty
b) <property name=… value=….>
- <bean name="u" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="9"></property>
- <property name="daoStatus" value="helloworld"></property>
- </bean>
1. <bean 中的scope属性
a) Spring_0600_IOC_Bean_Scope
b) singleton 单例 (默认)
c) proptotype 每次创建新的对象
- UserService service = (UserService)ctx.getBean("userService");
- UserService service1 = (UserService)ctx.getBean("userService");
- System.out.println(service == service1);
- <bean id="userService" class="com.demo.service.UserService" scope="prototype">
1. 集合注入
a) Spring_0700_IOC_Collections
b) 很少用,不重要!参考程序
- <?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.demo.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>
- <entry key="5" value="5"></entry>
- </map>
- </property>
- </bean>
- <bean id="userService" class="com.demo.service.UserService">
- <property name="userDAO" ref="userDAO"/>
- </bean>
- </beans>
1. 自动装配
a) Spring_0800_IOC_AutoWire
b) byName
c) byType
d) 如果所有的bean都用同一种,可以使用beans的属性:default-autowire
2. 生命周期
- <bean id="userDAO1" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="1"></property>
- </bean>
- <bean id="userDAO2" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="2"></property>
- </bean>
- <bean id="userService" class="com.demo.service.UserService" scope="prototype" autowire="byName">
- <property name="userDAO" ref="userDAO1"/>
- </bean>
1. 生命周期
a) Spring_0900_IOC_Life_Cycle
b) lazy-init (不重要)
c) init-method destroy-methd 不要和prototype一起用(了解)
1. Annotation第一步:
a) 修改xml文件,参考文档<context:annotation-config />
2. @Autowired
a) 默认按类型by type
b) 如果想用byName,使用@Qulifier
c) 写在private field(第三种注入形式)(不建议,破坏封装)
d) 如果写在set上,@qualifier需要写在参数上
3. @Resource(重要)
a) 加入:j2ee/common-annotations.jar
b) 默认按名称,名称找不到,按类型
c) 可以指定特定名称
d) 推荐使用
e) 不足:如果没有源码,就无法运用annotation,只能使用xml
4. @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
5. @Scope
6. @PostConstruct = init-method; @PreDestroy = destroy-method;
Annotation-based configuration
context.xml 标签自动提示配置
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
- <p>1. Annotation第一步:</p><p>a) 修改xml文件,参考文档<context:annotation-config /></p><p>1. @Autowired</p><p>a) 默认按类型by type</p><p>b) 如果想用byName,使用@Qulifier</p><p>c) 写在private field(第三种注入形式)(不建议,破坏封装)</p><p>d) 如果写在set上,@qualifier需要写在参数上</p>
- 在UserService 类里面
- @Autowired //把和你参数和对应的类型的的bean注入进来 默认的是byName 用的不多,会产生各种问题
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- (@Qualifier("u") // 可以指定 匹配那个名字的Bean 注入到参数里面来
- public void setUserDAO(@Qualifier("u")UserDAO userDAO) {
- this.userDAO = userDAO;
- }
1. @Resource(重要)
a) 加入:j2ee/common-annotations.jar
b) 默认按名称,名称找不到,按类型
c) 可以指定特定名称
d) 推荐使用
e) 不足:如果没有源码,就无法运用annotation,只能使用xml
@Resource //D:\javasoft\ssh\spring-framework-2.5.6\lib\j2ee\common-annotations.jar 包
1. @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
2. @Scope
3. @PostConstruct = init-method; @PreDestroy = destroy-method;
- <context:component-scan base-package="com.demo"></context:component-scan>
- package com.demo.dao.impl;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.model.User;
- @Component("u")
- public class UserDAOImpl implements UserDAO {
- @Override
- public void save(User u) {
- System.out.println("user save");
- }
- }
- package com.demo.service;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.dao.impl.UserDAOImpl;
- import com.demo.model.User;
- // 面向接口或者抽象编程,需要用谁直接在service 里面new谁 实现DAO接口即可
- //面向抽象编程就是 灵活
- @Component("userService")
- public class UserService {
- private UserDAO userDAO;
- public void init() {
- System.out.println("init");
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- @Resource(name="u") //常用
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public void add(User u) {
- this.userDAO.save(u);
- }
- public void destroy() {
- System.out.println("destroy");
- }
- }
Spring_1300_Annotation_Pre_Post_Scope
- package com.demo.service;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.dao.impl.UserDAOImpl;
- import com.demo.model.User;
- // 面向接口或者抽象编程,需要用谁直接在service 里面new谁 实现DAO接口即可
- //面向抽象编程就是 灵活
- @Component("userService")
- public class UserService {
- private UserDAO userDAO;
- @PostConstruct //构造完成之后
- public void init() {
- System.out.println("init");
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- @Resource(name="u") //常用
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public void add(User u) {
- this.userDAO.save(u);
- }
- @PreDestroy // 对象销毁之前指定这个方法
- public void destroy() {
- System.out.println("destroy");
- }
- }