Spring2.5
Foo.java FooStringname.
package com.tony.test;
publicclass Foo {
private String name;
public String toStirng(){
return"Foo Name is :" + this.name;
}
Setget
}
2 Bar.java BarStringadd.
package com.tony.test;
publicclass Bar {
private String add;
public String toStirng(){
return"Bar Add is :" + this.add;
}
Setget
}
3 Main.java MainFooBar
package com.tony.test;
publicclass Main {
private Foo foo;
private Bar bar;
public String toString(){
return"Main : [" + this.foo.toStirng() +" "+ this.bar.toStirng() + "]";
}
Setget
}
4 Spring-config-beans.xml
<bean id="main" class="com.tony.test.Main">
<property name="foo" ref="foo"></property>
<property name="bar" ref="bar"></property>
</bean>
<bean id="foo" class="com.tony.test.Foo">
<property name="name" value="Foo"></property>
</bean>
<bean id="bar" class="com.tony.test.Bar">
<property name="add" value="Bar"></property>
</bean>
5 Test.java TestSpringmain
package com.tony.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;
publicclass Test {
publicstaticvoid main(String[] args) {
String[] locations = {"spring-config-beans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
Main main = (Main) ctx.getBean("main");
system.out.println(main);
}
}
Test
Main : [Foo Name is :Foo Bar Add is :Bar]
1)Spring 2.5 package com.tony.test;
import org.springframework.beans.factory.annotation.Autowired;
publicclass Main {
@Autowired
private Foo foo;
@Autowired
private Bar bar;
public String toString(){
[1] [2] [3] 下一页