Spring的小窍门
2017-01-04 21:30
357 查看
Spring Tips
spring的annotation注解之@Resource@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解) Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。 @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。 @Resource装配顺序 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;
Spring 注解 @Resource和@Autowired
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下: @Autowired @Qualifier("userDao") private PersonDao personDao;
context:component-scan 配置详解
使用说明
Exclude subpackages from Spring autowiring?
<context:component-scan base-package="com.example"> <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/> </context:component-scan>
Spring中注入List,Set,Map,Properties
spring RestTemplate用法详解
Concurrency – Executors and Spring Integration
spring @component的作用
@controller 控制器(注入服务)
@service 服务(注入dao)
@repository dao(实现dao访问)
@component (把普通pojo实例化到spring容器中,相当于配置文件中的)
spring 中的和有什么区别
1.Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 2、区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcut和多个advice
How to inject null value in Spring
<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey"> <null /> </constructor-arg> </bean>
Spring 依赖注入:自动注入properties文件中的配置
自动注入需要使用@Value注解,这个注解的格式#{configProperties['mysql.url']}其中configProperties是我们在appContext.xml中配置的beanId,mysql.url是在properties文件中的配置项。
Spring3中用注解直接注入properties中的值
@Value("${sytem.username}")
相关文章推荐
- Spring 整合Struts的三个小窍门(一)
- Spring 整合Struts的三个小窍门(二)
- Spring 整合Struts的三个小窍门(三)
- 学习Java6(六) 嵌入式数据库Derby(7)Derby+spring+hibernate
- Struts2和Spring环境下action的单元测试
- Spring与Struts集成
- spring定时任务小例子
- Spring和Hibernate集成--声明式事务
- Spring 框架的设计理念与设计模式分析
- Spring的事务管理
- Spring生命周期深度剖析
- Junit框架使用(5)--JUnit与Spring集成
- spring的自动装配和精确装配
- spring 定时任务
- 1007--反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 第三个spring冲刺第10天
- Spring事件监听Demo
- 第十八章 springboot + thymeleaf
- SpringObjectFactory 空指针问题
- Spring使用Cache、整合Ehcache