2016-02-09 32 views
1

我是春季的新蜜蜂,在我的小測試程序中使用「Autowired」註釋。到目前爲止,我已經瞭解到,使「自動裝配Autowired」註釋工作,我們需要使用標籤由彈簧上下文XML打開它:是否有消除spring-context xml並以編程方式打開註釋?

<context:annotation-config /> 

我想知道是否有任何方式來消除XML和從程序中打開註釋。

這是我的春季計劃,正在工作在xml中定義的spring上下文。

SpringContext.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" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


      <context:annotation-config /> 

      <!-- bean definitions go here --> 

      <bean id="mainClass" class="com.myproject.spring.MyTester" /> 
      <bean id="student" class="com.myproject.spring.model.Student" scope="prototype" /> 
</beans> 

我的豆:

package com.myproject.spring.model; 



public class Student 
{ 
    private String name = "Johhn Hasel"; 


    public String getName() 
    { 
     return name; 
    } 


    public void setName(String name) 
    { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 

} 

而且我爲這個應用程序的主類:

package com.myproject.spring; 

import com.myproject.spring.model.Student; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MyTester 
{ 

    @Autowired 
    private Student student; 


    public static void main(String[] args) 
    { 

     ApplicationContext context = new ClassPathXmlApplicationContext("SpringContext.xml"); 
     MyTester mtster = context.getBean(MyTester.class); 

     System.out.println(mtster.student.toString()); 

    } 



} 

回答

1

如果你有你需要在最低至

  1. 註釋你的學生類@Component
  2. 標註您MyTester類@ComponentScan一個純粹獨立的應用程序和@Configuration
  3. 交易所帶有新的AnnotationConfigApplicationContext(MyTester。)的ClassPathXmlApplicationContext(..)。類)

什麼發生在幕後是

  1. 你標記你的學生爲可供自動發現
  2. 類設置MyTester類作爲起點,掃描組件和作爲類可能包含@Bean定義的配置
  3. 告訴Spring使用MyTester類中起點的註釋驅動配置
2

把你的servlet的XML文件如下:

<context-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

然後,您可以有一個Java類來定義您的配置將與@Configuration的註解。帶註釋的配置類的

例子:

@Configuration 
@ComponentScan(basePackages = "com.myproject.spring") 
@EnableWebMvc 
public class MvcConfiguration extends WebMvcConfigurerAdapter { 

    @Bean 
    public ViewResolver getViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/WEB-INF/pages/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

    @Bean 
    public Student student() { 
     return new Student(); 
    } 

} 

您還需要與@Component進行註解你的學生bean類。

例子:

@Component 
public class Student { 
... 
} 

如果你願意,你可以使用下面的,而不是定義在你的servlet的XML註釋配置方面是指你的bean的配置。

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MvcConfiguration.class);