以前非常流行使用XMl来配置
后来流行来用注解来代替XML
其实XML配置和注解都是一样的
无论xml还是注解,它们都是提供配置,只是载体不同
strut2的零配置:必须增加一个jar包 struts2-convention-plugin.jar(约定)
<action name=""class=“”method="">
<exception-mapping>异常机制
<interceptor-ref>
<param>
<result name="" type="">资源位置
<param>
</result>
</action>
@Action(value="name属性值",interceptorRefs={},params={},exceptionMappings={},results={})
@Result(name="",type="",params={})
@Result(name="",type="",location="")
spring的零配置
1.在配置文件中启用零配置
使用 <context:component-scan base-package="">
元素即可
2.为spring bean添加注解
spring的注解:
@Component:标注一个普通类的spring bean类,如果不指定id,默认的使用类名的小写类名首字母为id
<bean id="" class="">
@Controller:标注一个控制器组件类
@Service:标注一个业务逻辑组件类
@Repository:标注一个DAO组件类
–JavaEE中,使用以上3个注解,可以获得额外的好处
@Scope:指bean的作用域
相当于scope属性
@Resource:配置依赖注入
相当于ref属性,该注解支持filed注入
@Resource(name=“userSerivce”)就是将"userSerivce赋值给标记的filed的
@PostConstruct和@PreDestroy
@PostConstruct就相当于Init-methods属性
@PreDestroy就相当于destroy-method属性
@DependsOn:强制初始化其它bean
@Lazy:指定延迟初始化
lazy-init属性
@Authowired与@Qualifier自动装配,@Authowired默认是byType的自动装配
@Qualifier可指定byname的自动装配
相当于autowire属性
总结一些:spring推荐xml配置,spring的框架是解耦,将各个组件的依赖关系,从Java代码中提取到xml中管理,如果再使用零配置注解管理,从依赖关系管理又回到Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告诉spring到哪个包或者子包下搜索spring的bean -->
<context:component-scan basepackage=""
<bean id="userAction" class="org.fkjava.spring.action.UserAction">