我是春季的新蜜蜂,在我的小測試程序中使用「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());
}
}