spring bean 入门
2016-07-10 23:20
381 查看
参考:慕课网http://www.imooc.com/video/3668
一.spring 注入方式
spring注入是指实例化某个类时,对该类的成员变量赋值spring有两种注入方式(即两种赋值方式):
1.构造器注入:在构造函数中传入要初始化的成员变量的值,在构造函数值赋值
2.设值注入:调用该类的相应的成员变量的set方法对改成员变量赋值
public class InjectionServiceImpl implements InjectionService {//需要实例化的类 private InjectionDAO injectionDAO;//需要初始化的成员变量 //构造器注入--参数名称必须和配置文件中的name一致 public InjectionServiceImpl(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } //设值注入----set方法的名称必须和配置文件中的name一致,参数名称可以不一致 public void setInjectionDAO(InjectionDAO injectionDAO1) { this.injectionDAO = injectionDAO1; } …… }
spring配置文件:设值注入的name要与set方法的名称一致,set方法的参数名称名称可以不一样
构造注入的name要与构造函数的参数名称一致
<?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" > <!-- <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl"> --> <!-- <property name="injectionDAO" ref="injectionDAO"></property> --> <!-- </bean> --> <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl"> <constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg> </bean> <bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean> </beans>
3.自动注入-自动装配 全局default-autowire="xxx",单个 autowire=“xxx”
<bean>的autowire属性有如下六个取值,他们的说明如下:
1、 No:即不启用自动装配。Autowire默认的值。
2、 byName:在这种方式中要求bean的id与java类set方法名称一致。通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring
IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用set方法为其注入。ioc容器启动是就会检查byName方式的正确性
3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用set方法为其注入。可能抛出异常
4、 constructor:通byType一样,也是通过类型查找依赖对象。调用构造函数注入。可能抛出异常
5、 autodetect:在byType和constructor之间自动的选择注入方式。
6、 default:由上级标签<beans>的default-autowire属性确定。
注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default-autowire属性为byName,而在<bean>中定义为byType时,Spring IoC容器会优先使用<bean>标签的配置。
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139二.Bean的配置项
scope:实例的作用域
1.singleton:一个Bean容器(context)一个实例,这个实例会被保存到缓存中,并且对该bean的所有后续请求和引用都将返回该缓存中的对象实例,一般情况下,无状态的bean使用该scope。默认模式2.prototype:每次对该bean的请求都会创建一个新的实例,一般情况下,有状态的bean使用该scope。
web http相关
3.request:每个http请求将会有各自的bean实例,类似于prototype。
4.session:在一个http session中,一个bean定义对应一个bean实例。
5.global session:在一个全局的http session中,一般是在portlet context的时候有效。
三.Bean生命周期
bean的初始化与销毁通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
bean全局配置:default-init-method/default-destroy-method属性指定所有的bean初始化/销毁调用的操作方法。
在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用
如果前面三种都设置了,那么先调用接口的方法,在调用属性指定操作方法,全局的操作方法会屏蔽掉(没有实现全局的操作方法也不会出错)
四.Bean Aware接口
通过Bean 容器相关的资源,如BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实现BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。五.Spring 注解
1.注解扫描
转自:http://comeonbabye.iteye.com/blog/1211425Spring配置文件 applicationContext.xml的<context:component-scan >标签用途比我想像的还要实用。而且后来才知道,有了<context:component-scan >,另一个<context:annotation-config/>标签根本可以移除掉,因为被包含进去了。原本我survery
Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下寻找有@Component和@Configuration的target Class。而现在如下的:
<context:component-scan base-package="com.foo" use-default-filters ="false"> <context:include-filter type="regex" expression="com.foo.bar.*Config"/> <context:include-filter type="regex" expression="com.foo.config.*"/> </context:component-scan > |
filter标签在Spring3有五个type,如下:
Filter Type | Examples Expression | Description |
annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
assignable | org.example.SomeClass | 指定class或interface的全名 |
aspectj | org.example..*Service+ | AspectJ语法 |
regex | org\.example\.Default.* | Regelar Expression |
custom | org.example.MyTypeFilter | Spring3新增自订Type,实作org.springframework.core.type.TypeFilter |
2.@component
把类实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> id为类名-第一个字母小写
3. @Autowired
使用 @Autowired的地方都是需要注入
Bean 的,它可以用在成员变量、构造函数、setter方法。
@Autowired(required = false)表示不是一定要注入,如果依赖对象为null就不用注入了
可以注解众所周知的spring的解析依赖性接口:如ApplicationContex、BeanFactory
还可以通过注解成员变量、方法注入特定类型的集合(set,list,map)---符合该类型的所有bean(如果想要集合有序可以加@Order)
不能注解BeanPostProcessor、BeanFactorPostProcessor类型,因为Autowired本身就是有spring
BeanPostProcessor处理的,这些类型只能通过xml或者@Bean来注入
如:
@Autowired
public func(set<xxx> para){ }
@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区 别的。首先来看一下:
a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;适用于成员变量、只有一个参数的setter方法;所以目标是构造函数或者,多参数方法时最好使用@Autowired+@Qualifier
b。@Autowired默认是按照类型装配注入的,如果想按照名称id来转配注入,则需要结合@Qualifier一起使用(1.可以写在方法参数类型前来限定参数类型;2.可以用在成员变量上);
c。@Resource注解是又J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用
@Resource的方式;
d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上
4.mvc 注解:@ Repository
@Serivce @ Control
5.@Bean
@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。6.配置文件读取
config.xml中设置<context:property-placeholder location="classpath:/config.properties"/>(config.properties文件存放实际的key/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" xmlns:context="http://www.springframework.org/schema/context" 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" > <context:property-placeholder location="classpath:/config.properties"/> </beans>法一:java文件中采用注解的方式获取值
@Configuration @ImportResource("classpath:config.xml") public class StoreConfig { @Value("${url}") private String url; @Value("${jdbc.username}") private String username; @Value("${password}") private String password; @Bean public MyDriverManager myDriverManager() { return new MyDriverManager(url, username, password); } }法二:在config.xml文件中添加
<bean
id="myDriverManagere" class="……">
<property name="url" value="${url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${password}"/>
</bean>
7.spring 3.0 jsr33
@Inject等效于@Autowired可以用于属性、方法、成员变量
@name 等效于@component 可以指定name即指定bean的id
相关文章推荐
- 使用java.util.concurrent包处理多线程
- Java HashMap工作原理及实现
- JAVA_连接池、DataSource、JNDI
- java-----ArrayList的fail-fast机制学习
- Java线程
- 渐析java的浅拷贝和深拷贝
- Java泛型使用小结
- java动态代理(JDK和cglib)
- java基础/map/hasmap/linkedhasmap/treemap/collections工具类
- (转)如何使用Java、Servlet创建二维码
- 学习javaee第四天的加强训练
- (转)Java经典设计模式(3):十一种行为型模式(附实例和详解)
- springmvc mybatis 手动提交事务
- (转)Java经典设计模式(2):七大结构型模式(附实例和详解)
- (转)Java经典设计模式(1):五大创建型模式(附实例和详解)
- java.lang.OutOfMemoryError问题与heap获取
- java中的static详解
- Java-CipherInputStream与CipherOutputStream 加密流
- JAVA运行时内存模型
- Java为什么只能单继承?