spring的一些注解
2015-06-01 19:15
465 查看
spring的一些注解
要使用spring注解首先肯定要用scanner,spring配置文件中要有
其中com.neu就是要扫描的包
但是必须有下面的xmlns:context以及最后3个schema
要使用spring注解首先肯定要用scanner,spring配置文件中要有
[code]<context:annotation-config /> <context:component-scan base-package="com.neu" />
其中com.neu就是要扫描的包
但是必须有下面的xmlns:context以及最后3个schema
[code]<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">[/code]
然后项目中就可以使用注解了
1、@Controller用于标注控制层组件(如struts中的action) ,控制器(注入服务)
2、@Service用于标注业务层组件 ,service 服务(注入dao)
3、@Repository用于标注数据访问组件,即DAO组件,@repository dao(实现dao访问)
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 @component (把普通pojo实例化到spring容器中,相当于配置文件中的)
被注解的类会纳入进spring容器中管理。
还可以自己写个Servlet代理[code]/** * 我们自己实现的一个代理类用于将Servlet转为Spring管理的Servlet Bean */ public class ServletToBeanProxy extends GenericServlet { private String targetBean;//当前客户端请求的Servlet名字 private Servlet proxy;//代理Servlet @Override public void init() throws ServletException { super.init(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); //初始化Spring容器 this.targetBean = getServletName(); this.proxy = (Servlet) wac.getBean(targetBean);//调用ServletBean proxy.init(getServletConfig());//调用初始化方法将ServletConfig传给Bean } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { proxy.service(arg0, arg1);//在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post) } }
然后web.xml中将所有的servlet都写成这样的:[code] <servlet> <servlet-name>ServantInfoAction</servlet-name> <servlet-class>com.gjp.o2o.controller.ServletToBeanProxy</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServantInfoAction</servlet-name> <url-pattern>/ServantInfoAction</url-pattern> </servlet-mapping>
就是将普通的servlet转移到这个代理。
然后所有普通的servlet都使用@controller就实现了
相关文章推荐
- Struts2中 Result类型配置详解
- Java的static
- java Map及Map.Entry详解及用途
- java对象引用在方法中传递
- java个人学习笔记:面向对象的思想
- Spring Aop实例
- java并发之原子性与可见性
- spring在web容器启动时执行初始化方法
- Spring组件扫描<context:component-scan/>使用详解
- java中long和double类型操作的非原子性探究
- SpringMVC返回类型
- 黑马程序员——JDK1.5新特性
- java反射机制
- Java 静态内部类
- Xposed出现 java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
- java中栈的简单操作
- Spring学习笔记——关于Spring注解扫描不能注入new对象问题
- Java for LeetCode 131 Palindrome Partitioning
- 新手 java 的反射
- JDK的动态代理-多接口