1使用普通注解 添加
<context:annotation-config />
2使用@Component 添加
<context:component-scan base-package="com.bjsxt"/>
进行包扫描
3使用AOP添加
<aop:aspectj-autoproxy />
4 <context:annotation-config/> 的作用【此次还有待验证,是否符合3.0特性】
<context:annotation-config/>
他的作用是隐式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor、
RequiredAnnotationBeanPostProcessor
5 @Repository , @Service , @Controller 和 @Component 四者几乎没有区别,根据名字来看 分别存储 dao/service/表现层/那层都可用
理论上虽如此,如果用spring mvc 在进行跳转中还是必须用 @Controller 这个的,不然会找不到 路径【已经过验证】
6 使用Servlet+spring 获取 项目上下文文档的的代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
Service service = (Service)ctx.getBean("service");
service.service();
}
7 使用注解@Resource的方法
1)配置文件中添加一句 <context:annotation-config />
2) 在service 配置文件少写了dao参数
3) 在service类中不必写dao 的set/get方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--<mvc:annotation-driven /> -->
<context:annotation-config />
<bean id="dao" class="com.zj.Dao"></bean>
<bean id="service" class="com.zj.Service">
</bean>
</beans>
8 如果想更精简代码呢,可以使用 @Component 加在类 dao和service上这同一级别的几个注解,在配置文件中加上一个扫描包 则可以减少dao 和service的bean配置,
而且发现一个奇怪的问题 ,如果添加了 包扫描 ,可以去掉
<context:annotation-config /> 这句的配置了,虽然验证了一次,还是不太相信,先记录下来