private UserDao userDao;
不用写set get就能注入,当然,前提是你已经开启了注解功能。
2、<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型表达式范例 说明
注解:org.example.SomeAnnotation将所有使用SomeAnnotation注解的类过滤出来
类名指定:org.example.SomeClass过滤指定的类
正则表达式:com\.kedacom\.spring\.annotation\.web\..*通过正则表达式过滤一些类
AspectJ表达式:org.example..*Service+通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>
值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了.
3、
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring/spring-*.xml,
classpath*:/spring/web/spring-web-*.xml
</param-value>
</context-param>
要想加载contextConfigLocation属性的值,需要加下面的spring的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果你的spring配置文件不放在context-param中,像这种方式的配置可以不配置该监听器:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring/spring-web-mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4、<context:annotation-config/>和<context:component-scan
AutowiredAnnotationBeanP
PersistenceAnnotationBea
注册这4个
例如:
如果你想使用@Autowired注解,那么就必须事先在
<bean
如果想使用@ Resource
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBea
如果想使用
<bean
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
<context:component-scan