Spring IOC
Spring Overview
The following content only explains the basic use of spring IOC
spring IOC: dependency injection
Spring AOP: Manage component objects and maintain object relationships. Purpose: Reduce component coupling
Spring web MVC: MVC design: building an MVC structured WEB application
Spring integrates other technologies: JDBC, Mybatis, Hibernate, Struts, etc.
Spring IOC applications:
Application of objects by injection to achieve component decoupling
a. Manage objects: create, initialize, release resources, destroy
b. Maintain object relations: Establish object relations by injection Dependency Injection(DI)(Dependency Injection: set method injection, constructor injection)
c. Build SpringIOC development environment: introduce relevant jar packages; add ApplicationContext.xml to src
Spring Container-> Managing Components and Object Relationships
1. Create ApplicationContext object
2. Configure to applicationContext.xml
3. Using ApplicationContext object getBean()
Instantiate ApplicationContext container object-> applicationContext.xml
二.Spring创建Bean对象的控制
1.控制对象创建方式(使用范围)
在元素中使用scope属性控制,scope可以支持singleton和prototype,默认值是singleton。
该组件在Spring容器中只有一个bean对象,ac.getBean("id")无论调用getBean();多少次都返回同一个对象;
scope为prototype则每次ac.getBean("id");返回一个新的对象。
2.指定对象初始化方法
利用元素的init-method指定,当创建bean对象后自动执行init-method方法。
3.指定对象销毁方法
利用元素的destroy-method指定。
满足下面条件才有效:
-组件对象为单例模式
-调用AbstractApplicationContext容器对象的close()方法
4.控制单例对象创建时机
在默认情况下,单例对象是Spring容器创建时实例化;可以使用元素的lazy-init=true属性将创建时机推迟到getBean()方法调用时。
public class ExampleBean { public ExampleBean() { System.out.println("--创建ExampleBean--"); } public void init() { System.out.println("init.."); } public void destroy() { System.out.println("destory object..."); } public void executor(){ System.out.println("调用executor方法"); }}public class TestBean { public static void main(String[] args) { //创建Spring容器对象 String conf = "applicationContext.xml"; AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf); //从spring容器获取e1,e2 ExampleBean e1 = ac.getBean("e1", ExampleBean.class); e1.executor(); ExampleBean e2 = ac.getBean("e1", ExampleBean.class); System.out.println(e1 == e2);//true ExampleBean e3 = ac.getBean("e2", ExampleBean.class); ExampleBean e4 = ac.getBean("e2", ExampleBean.class); System.out.println(e3 == e4);//false ac.close();//释放Spring容器对象,自动调用单例的destory-method }}
IOC: Inversion of Control控制反转
改变了对象获取方式,之前编码方式采用new构造方式获取对象;IOC中采用了由容器创建对象之后注入进来使用。只要修改配置就可以改变对象关系。
三.自动装配(自动注入)
1.自动注入(autowire)
用于指定自动注入规则。可以使用byType,byName,constructor等。用于简化注入配置。
使用byType类型匹配注入需要注意,有2个及其以上匹配会出现异常。
2.各种类型信息的注入配置格式
a).注入字符串,数值单个数值
*b).注入bean对象
c).注入集合list,set,map,properties
tom jack hongkong shanghai root 123456 com.mysql.jdbc.Driver
d).spring表达式注入
#{表达式}
#{id名.属性}或#{id名.key}
如果时对象属性,需要有getXXX方法
#{List对象id[0]}
小树 静汪 hongkong shanghai #{dbParams.user} #{dbParams.password} #{dbParams.databases}
db.properties:
=====
3.利用注解配置应用IOC
在JDK5.0时追加一些新特性
注解:在类定义,方法定义,成员变量定义前面使用,格式为@注解标记名。
a).组件自动扫描
可以按指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,会将组件扫面倒Soring容器。
@Component //其他组件
@Controller //控制层组件
@Service //业务层组件xxxService
@Repository //数据访问层组件xxxDao
@Named(需要引入第三方标准包)
@Scope控制对象创建,默认单例
@PostConstruct指定init-method
@PreDestroy指定destroy-method
b).注入注解
@Resource:可以在变量定义前或setXXX方法前应用
@AutoWired:可以在变量定义前或setXXX方法前应用
一般使用时,功能等价,都可以实现注入。
如果不存在多个匹配类型,使用@Resurce或@Autowired都可以。
如果存在多个匹配类型,建议按名称注入
@Resource(name="指定名称")或
@Autowired
@Qualifier("p")
使用建议:set注入建议用@Resource,构造器建议用@Autowired
自己编写的组件建议使用注解配置;框架API只能用XML配置。
spring配置:
代码:@Component //扫描ExampleBean组件,默认id=exampleBean//@Scope("prototype")//等价于@Scope("singleton")//等价于public class ExampleBean { @PostConstruct //等价于 public void init(){ System.out.println("初始化逻辑"); } public void execute(){ System.out.println("---执行execute处理方法---"); } @PreDestroy //scope为singleton并释放容器资源时,等价于 public void destroy(){ System.out.println("释放资源"); }}@Component("exam1")public class ExampleBean1 { public void execute(){ System.out.println("---执行exampleBean1的execute---"); }}
test
public class ExampleBeanTest { public static void main(String[] args) { String conf = "applicationContext.xml"; AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf); ExampleBean e1 = ac.getBean("exampleBean", ExampleBean.class); e1.execute(); ExampleBean1 e2 = ac.getBean("exam1", ExampleBean1.class); e2.execute(); ExampleBean ee = ac.getBean("exampleBean", ExampleBean.class); System.out.println(e1.toString()); System.out.println(ee.toString()); ac.close(); }}
运行结果:
---执行execute处理方法---
---执行exampleBean1的execute---
springIOC.ExampleBean@548a102f
springIOC.ExampleBean@548a102f
释放资源