spring的IoC和DI详解
这里先来简单介绍下IoC和DI的区别: IOC:翻译过来是控制反转,将对象的创建权由Spring管理,HelloService不需要自己去创建,Spring可以帮你创建。 DI:依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。 我们现在编写的类是没有其它的属性的,如果你学过UML这种设计的话,面向对象中对象中的几种关系 简单来书,IoC更像是一种思想,DI是一种行为。 另一种说法是,ioc是目的,di是手段。ioc是指让生成类的方式由传统方式(new)反过来,既程序员不调用new,需要类的时候由框架注入(di),是同一件不同层面的解读。 IoC:Inverse of Control(控制反转): 将原本在程序中手动创建对象的控制权,交由Spring框架来管理。 Spring第一个spring测试程序 1.准备jar包 spring-beans-4.1.2.RELEASE.jar public class HelloSpring { @Setter //必须有set属性 private String name; public void say() { System.out.println("hello---" + name); } } @Test //把创建的对象交给spring框架管理 public void test1() throws Exception { //1读取资源文件 Resource resource = new ClassPathResource("/applicationContext.xml"); //2创建spring容器对象 BeanFactory bf = new XmlBeanFactory(resource); //3从srping容器中获得指定名称对象 HelloSpring hello = bf.getBean("helloSpring",HelloSpring.class); 配置applicateion.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.xsd"> <beans> <bean name="helloSpring" class="全限定类名"> <property name="需要注入的属性名称" value="注入值" /> </bean> </beans> 使用import元素引入其他的配置文件: <import resource="classpath:bin目录文件路径"/> 使用import元素注意: 1、默认情况下,从classpath的跟路径寻找。 ①:[classpath:]:后面的文件从classpath路径开始找(推荐);[注意classloader的问题。] 注意:只有当框架中实现了Resource接口才能够识别上述的前缀标识符。 Spring中的测试 Spring测试环境准备: 依赖jar: 1.spring-test-4.1.2.RELEASE.jar Spring4.x需要依赖的单元测试得是最新的junit4.12,Eclipse自带的junit4.8不支持,同时从Spring4.x开始,还得依赖AOP包的支持。 junit-4.12.jar @RunWith(SpringJUnit4ClassRunner.class)表示先启动Spring容器,把junit运行在Spring容器中 @ContextConfiguration("classpath:applicationContext.xml"):表示从哪里加载资源文件 public class HelloWorldTest { @Autowired :表示自动装配 private BeanFactory factory; @Test public void testSpringTest() throws Exception { HelloWorld helloWorld = factory.getBean("springTest",HelloWorld.class); helloWorld.sayHello(); } } 把@ContextConfiguration(“classpath:applicationContext.xml”) 写成@ContextConfiguration Spring容器 BeanFactory:是Spring中最底层的接口,只提供了最简单的IoC功能(创建和管理bean)。 在应用中,一般不使用BeanFactory,而推荐使用ApplicationContext(应用上下文),原因如下。 被Spring所管理的成员都称之为bean(类,对象,bean元素). BeanFactory和ApplicationContext的区别 1、ApplicationContext继承了BeanFactory,拥有了基本的IoC功能; ①、支持国际化; bean的创建时机: 1.ApplicationContext在加载的时候就会创建所有的bean(Web应用建议). //针对于当前xml中所有的bean:是否需要延迟初始化. <bean lazy-init="default | false | true"> //针对于指定的bean:是否需要延迟初始化. <beans default-lazy-init="default | false | true"> bean的作用域,bean对象可以存活多久 <bean id="" class="" scope="作用域"/> singleton: 单例 ,在Spring IoC容器中仅存在一个Bean实例 (默认的scope) prototype: 多例 ,每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean():不会在容器启动时创建对象 request: 用于web开发,将Bean放入request范围 ,request.setAttribute("xxx") , 在同一个request 获得同一个Bean session: 用于web开发,将Bean 放入Session范围,在同一个Session 获得同一个Bean globalSession: 一般用于Porlet应用环境,分布式系统存在全局session概念(单点登录),如果不是porlet环境,globalSession 等同于Session 对于Struts2中的Action使用prototype类型,其他使用singleton DI:Dependency Injection: <bean />元素的:autowire属性 <bean id="somebean" class="SomeBean全限定名" autowire="byType"/> autowire属性:让spring按照一定的方式自己去找合适的对象,并完成DI - default:不要自动注入 注意: 1,如果按照byName自动注入,要求所有的属性名字和id的名字必须保证一种规范的命名方式; 手动装配: 属性注入: 通过对象的setter方法注入依赖的对象. 使用setter注入: //实体类 @Setter public class Employee { private String name; } //测试类 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class EmployeeTest { @Autowired private ApplicationContext ctx; @Test public void test1() throws Exception { Employee bean = ctx.getBean("employee",Employee.class); System.out.println(bean); } } //application配置 <bean id="employee" class="com.***.Employee"> <property name="name" value="zoe" /> </bean> 构造器注入: 通过构造器,在创建对象的时候就注入依赖对象 constructor-arg:构造器参数 1,spring在实例化对象的时候,如果对象没有配置constructor-arg,则使用默认的构造器实例化对象 使用哪种注入方式比较好(setter?构造器?)? 1,如果一个类必须依赖另一个类才能正常运行,用构造器; public class Employee { private String name; public Employee(String name) { this.name = name; } } //application配置 <bean id="employee" class="com.***.Employee"> <constructor-arg name="name" value="zoe" /> </bean> 属性占位符(property place holder) spring属性占位符之创建连接池对象 依赖的jar:>>MySQL驱动包>>druid包. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class PropertyPlaceHolderTest { @Autowired private DataSource ds ; @Test public void testLink() throws Exception { Connection conn = ds.getConnection(); String sql = "select * from user"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); } } application配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 加载 properties --> <!-- system-properties-mode="NEVER" 不使用系统默认属性 --> <context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER" /> <!-- 配置连接池 --> <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbd.driverClassName}" /> <property name="url" value="${jdbd.url}" /> <property name="jdbd.username" value="${username}" /> <property name="jdbd.password" value="${password}" /> </bean> </beans> db.properties文件 jdbd.driverClassName=com.mysql.jdbc.Driver jdbd.url=jdbc:mysql:///springdemo jdbd.username=root jdbd.password=admin 总结 以上就是本文关于spring的IoC和DI详解的全部内容,希望对大家有所帮助。欢迎参阅:Java探索之Thread+IO文件的加密解密代码实例、spring配置扫描多个包问题解析、浅谈Springboot之于Spring的优势等,有什么问题可以随时留言指出,感谢大家! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |