浅谈SPRING的注释注入

Spring2.5的注释注入Bean已经成为众多Spring爱好者的首选(3.0版本和2.5基本差不多),但是在使用过程中可能会出现很多的问题,笔者找了网上很多的资料,没有一个真正完整使用注释来完成一个系统,这里给笔者的使用心得及遇到主要棘手的问题贴上来,和大家共勉!
相信大家使用Spring2.5主要都是为了注释的应用,尤其是@Controller和@Service、@Component、@Resource这些标签的应用,因为这些标签替代了更多的XML配置,使用Spring2.5注释来完成这些工作会带来很多的便利,当然初次使用肯定会遇到很多的原因!
使用@Controller标签的时候需要注意配置步骤和配置文件的写法,首先配置web.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.jhtml</url-pattern>
</servlet-mapping>
</web-app>

web.xml中在context-param参数中加载applicatonContext.xml,不要再init-param中加载,因为Spring加载扫描注释Bean有个顺序
写好web.xml后需要在web.xml的同等目录下建立一个text-servlet.xml配置文件,代码如下:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.javatalker"/>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean id="velocityCongfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/apps/web/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding ">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.velocity.VelocityView" />
<property name="contentType">
<value>text/html;charset=UTF-8</value>
</property>
</bean>
</beans>


在这个配置文件中主要配置spring需要扫描的包、开启Spring注释功能的识别、设定Spring视图,这样就可以了
然后写一个applicationContext.xml,代码如下:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 使Spring关注Annotation -->
<context:annotation-config />
</beans>


在这个配置文件中只需要一行代码,用来告诉Spring启用注释配置。
这样就可以使用@Controller的注释来完成url的导向了,举例如下:
@Controller
public class UserAction {

@RequestMapping("/user/save.jhtml")
public ModelAndView Save(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("save.html");
}
}


如上代码,要注意的是UserAction是个单独的class,不继承Spring任何类,不像以前需要继承org.springframework.web.servlet.mvc.multiaction.MultiActionController,使用@Controller注释后,就可以在方法级上来完成对应的url,使用@RequestMapping来完成这项工作,比如上面的注入表示,访问UserAction中save方法就可以使用:/user/save.jhtml这样的url,这样的url比user.jhtml?method=save更加友好了吧!
关于@Controller的说明到这里就差不多了!

其次要强调的是注释注入Bean的应用,使用@Component这些注释可以标注一个class为Spring管理中的Bean,但是有些Bean不是class能够解决的,必须使用配置文件来完成,比如DataSource等,这样就需要使用Spring的配置文件来完成,Spring2.5开始在加载Bean的时候首先扫描xxx-servlet.xml中配置的Bean,然后扫描使用注释标注的Bean,这样一来如果给datasource这些bean配置在applicatonContext.xml中,而在某个Bean中使用@Autowired注入datasource就会出错,解决的方法很简单,就是给配置的Bean放到xxx-servlet.xml,个人感觉这个也是Spring2.5后不好的地方,相信以后很改正过来的!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值