spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置。
注意:用@Configuration注解该类,等价 与XML中配置beans;
用@Bean标注方法等价于XML中配置bean。
注意:这个配置类,相当于xml文件的作用
bean:
package codeEETest;
public interface Person
{
// 定义一个使用斧子的方法
public void useAxe();
}
package codeEETest;
public interface Axe
{
// Axe接口里有个砍的方法
public String chop();
}
package codeEETest;
public class Chinese implements Person
{
private Axe axe;
private String name;
// axe的setter方法
public void setAxe(Axe axe)
{
this.axe = axe;
}
// name的setter方法
public void setName(String name)
{
this.name = name;
}
// 实现Person接口的useAxe()方法
public void useAxe()
{
// 调用axe的chop()方法,表明Person对象依赖于axe对象
System.out.println("我是:"+ name + axe.chop());
}
}
实现类1:
package codeEETest;
public class SteelAxe implements Axe
{
public String chop()
{
return "钢斧砍柴真快";
}
}
实现类2:
package codeEETest;
public class StoneAxe implements Axe
{
public String chop()
{
return "石斧砍柴好慢";
}
}
以下采用两种方式解决依赖:chinese类中依赖axe对象(axe对象的方法)
方式一:xml
<bean id="chinese" class="codeEETest.Chinese">
<property name="axe" ref="stoneAxe"/>
<property name="name" value="孙悟空"/>
</bean>
<bean id="stoneAxe" class="codeEETest.StoneAxe"/>
<bean id="steelAxe" class="codeEETest.SteelAxe"/>
这里选择依赖了stoneAxe实现类
test:
public class BeanTest
{
public static void main(String[] args)throws Exception
{
// 创建Spring容器
// ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
ApplicationContext ctx = new ClassPathXmlApplicationContext("codeEETest/beans.xml");
// 获取chinese 实例
Person p = ctx.getBean("chinese" , Person.class);
// 调用useAxe()方法
p.useAxe();
}
}
结果:
我是:孙悟空石斧砍柴好慢
方式二:配置类
package codeEETest;
import org.springframework.context.annotation.*;
import org.springframework.beans.factory.annotation.*;
@Configuration
public class AppConfig
{
@Value("孙悟空") String personName;
// 配置一个Bean:chinese
@Bean(name="chinese")
public Person person()
{
Chinese p = new Chinese();
p.setAxe(stoneAxe());
p.setName(personName);
return p;
}
// 配置Bean:stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe()
{
return new StoneAxe();
}
// 配置Bean:steelAxe
@Bean(name="steelAxe")
public Axe steelAxe()
{
return new SteelAxe();
}
}
package codeEETest;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.springframework.context.annotation.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
// 创建Spring容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
// ApplicationContext ctx = new ClassPathXmlApplicationContext("codeEETest/beans.xml");
// 获取chinese 实例
Person p = ctx.getBean("chinese" , Person.class);
// 调用useAxe()方法
p.useAxe();
}
}
结果:
我是:孙悟空钢斧砍柴真快
-----------------------------------------------------------------------------------------------------------------
总结:
(1)配置类起到了替代xml的作用,是以java类的形式创建类和设置依赖。可以不用反射,new出对象。
(2)二者可以同时存在混合使用(见下)。
-----------------------------------------------------------------------------------------
使用java配置类时,三个常用的annotation:
(1)@Configuration:修饰一个java配置类
(2)@Bean:修饰一个方法,该方法返回值sing一成容器的一个bean
(3)@Value:修饰一个字段,配置变量
此外还有@Import:修饰配置类,用于向当前java配置类中导入其他配置类
@Scope:用于修饰方法,指定生命域
......
-------------------------------------------------------------------------------------
那么,如何共同使用xml和配置类呢?(上边的实验实际上是各自有各自的ApplicationContext)
(1)如果是以xml为主,就要让xml能加载配置类,只需在xml加:
<bean class="codeEE.AppConfig" />
(2)如果是配置类为主,可增加注解:
@Configuration
@ImportResource("classpath:/codeEE/bean.xml")
public class AppConfig
{
...