Spring配置方式

将Spring配置到应用开发中有以下三种方式:

  1. 基于XML的配置
  2. 基于注解的配置
  3. 基于Java的配置

基于XML配置 

Spring框架中,依赖和服务需要在专门的配置文件来实现,常用XML格式的配置文件。这些配置文件的格式通常用<beans>开头,然后一系列的bean定义和专门的应用配置选项组成。

Spring XML配置的主要目的是使所有的Spring组件都可以用xml文件的形式来进行配置。这意味着不会出现其他的Spring配置类型(比如声明的方式或基于Java Class的配置方式)。

SpringXML配置方式是使用Spring命名空间所支持的一系列的XML标签来实现的。Spring有以下主要的命名空间:contextbeansjdbctxaopmvcaso

<beans>
    <!--JSON Support-->
    <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
</beans>

下面这个web.xml仅仅配置了DispatcherServlet,这件最简单的配置便能满足应用程序配置运行时组件的需求。

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

基于注解配置 

通过在相关的类,方法或字段声明上使用注解,将bean描述转移到组件类的内部,而不是使用XML来描述bean装配。注解注入将会被容器在XML注入之前被处理,所以后者会覆盖掉前者对于同一个属性的处理结果。默认情况下,Spring容器中未打开注解装配。因此,您需要在使用它之前在Spring配置文件中启用它。例如:

<beans>
    <context:annotation-config/>
    <!-- bean definitions go here -->
</beans>

下面是几种比较重要的注解类型:

  1. @Required:该注解应用于设值方法。
  2. @Autowired:该注解应用于有值设值方法、非设值方法、构造方法和变量。
  3. @Qualifier:该注解和@Autowired注解搭配使用,用于消除特定bean自动装配的歧义。
  4. @Resource

基于Java配置 

SpringJava配置的支持是由@Configuration注解和@Bean注解来实现的。由@Bean注解的方法将会实例化、配置和初始化一个新对象,这个对象将由Spring的IoC容器来管理。@Bean声明所起到的作用与<bean/>元素类似。被@Configuration所注解的类则表示这个类的主要目的是作为bean定义的资源。被@Configuration声明的类可以通过在同一个类的内部调用@bean方法来设置嵌入bean的依赖关系。

最简单的@Configuration声明类请参考下面的代码:

@Configuration
public class AppConfig{
    @Bean
    public MyService myService(){
        return new MyServiceImpl();
    }
}

对于上面的@Beans配置文件相同的XML配置文件如下:

<beans>
    <bean id="myService" class="com.somnus.services.MyServiceImpl"/>
</beans>

上述配置方式的实例化方式如下:利用AnnotationConfigApplicationContext类进行实例化

public static void main(String[] args){
    ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);
    MyService myService=ctx.getBean(MyService.class);
    myService.doStuff();
}

要使用组件扫描,仅需用@Configuration进行注解即可:

@Configuration
@ComponentScan(basePackages="com.somnus")
public class AppConfig{
    ...
}

在上面的例子中,com.somnus包首先会被扫到,然后再容器内查找被@Component声明的类,找到后将这些类按照Sring bean定义进行注册。

如果你要在你的web应用开发中选用上述的配置的方式的话,需要用AnnotationConfigWebApplicationContext类来读取配置文件,可以用来配置Spring的Servlet监听器ContextLoaderListener或者Spring MVC的DispatcherServlet。

<web-app>
    <!--Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext-->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <!--Configuration locations must consist of one or more comma-or space-delimited fully-qualified @Configuration classes. Fully-qualified packages may also be specified for component-scanning-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.howtodoinjava.AppConfig</param-value>
    </context-param>

    <!--Bootstrap the root application context as usual using ContextLoaderListener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--Declare a Spring MVC DispatcherServlet as usual-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext-->
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>

        <!--Again, config locations must consist of one or more comma-or space-delimited and fully-qualified @Configuration classes-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.howtodoinjava.web.MvcConfig</param-value>
        </init-param>
    </servlet>

    <!--map all requests for/app/* to the dispatcher servlet-->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
</web-app>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值