有没有办法消除 spring-context xml 并以编程方式打开注释?
Is there anyway to eliminate spring-context xml and turn on annotations programatically?
我是 spring 的新蜜蜂,正在我的小测试程序中使用 'Autowired' 注释。到目前为止,我了解到要使 'Autowired' 注释起作用,我们需要使用标签从 spring- 上下文 xml 中打开它:
<context:annotation-config />
我想知道是否有任何方法可以消除 xml 并从程序中打开注释。
这是我的 spring 程序,它正在 与 spring 中定义的上下文一起工作 xml .
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;
}
}
我的主要 class 此应用程序:
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());
}
}
将以下内容放入您的 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 class 来定义您的配置,该配置将使用 @Configuration 注释进行注释。
带注释的配置示例 class:
@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 注释来注释您的 Student bean class。
示例:
@Component
public class Student {
...
}
如果您愿意,您可以使用下面的方法引用您的 bean 配置,而不是在您的 servlet 中定义注释配置上下文 xml。
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MvcConfiguration.class);
如果您有一个纯独立的应用程序,您至少需要
- 用@Component
注释你的学生class
- 使用@ComponentScan 和@Configuration
注释您的 MyTester class
- 将 ClassPathXmlApplicationContext(..) 替换为新的 AnnotationConfigApplicationContext(MyTester.class)
幕后发生的事情是
- 您将您的学生标记为 class 可用于自动发现
- 将 MyTester class 设置为扫描组件的起点和 class 其中可能包含 @Bean 定义之类的配置
- 告诉 Spring 使用带有 MyTester 起点的注释驱动配置 class
我是 spring 的新蜜蜂,正在我的小测试程序中使用 'Autowired' 注释。到目前为止,我了解到要使 'Autowired' 注释起作用,我们需要使用标签从 spring- 上下文 xml 中打开它:
<context:annotation-config />
我想知道是否有任何方法可以消除 xml 并从程序中打开注释。
这是我的 spring 程序,它正在 与 spring 中定义的上下文一起工作 xml .
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;
}
}
我的主要 class 此应用程序:
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());
}
}
将以下内容放入您的 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 class 来定义您的配置,该配置将使用 @Configuration 注释进行注释。
带注释的配置示例 class:
@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 注释来注释您的 Student bean class。
示例:
@Component
public class Student {
...
}
如果您愿意,您可以使用下面的方法引用您的 bean 配置,而不是在您的 servlet 中定义注释配置上下文 xml。
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MvcConfiguration.class);
如果您有一个纯独立的应用程序,您至少需要
- 用@Component 注释你的学生class
- 使用@ComponentScan 和@Configuration 注释您的 MyTester class
- 将 ClassPathXmlApplicationContext(..) 替换为新的 AnnotationConfigApplicationContext(MyTester.class)
幕后发生的事情是
- 您将您的学生标记为 class 可用于自动发现
- 将 MyTester class 设置为扫描组件的起点和 class 其中可能包含 @Bean 定义之类的配置
- 告诉 Spring 使用带有 MyTester 起点的注释驱动配置 class