xwork依赖注入示例

package xworktest;
/**
* @author wangmingjie
* @date 2008-9-13上午11:13:38
*/
public interface IPerson {
public void say();
}

====================================

package xworktest;
/**
* @author wangmingjie
* @date 2008-9-13上午11:14:33
*/
public class Person implements IPerson {

public void say() {
System.out.println("这是:person");
}

}

====================================

package xworktest;
/**
* @author wangmingjie
* @date 2008-9-13上午11:14:00
*/
public interface IPersonService {
public void say();
}

====================================

package xworktest;

import com.opensymphony.xwork2.inject.Inject;

/**
* @author wangmingjie
* @date 2008-9-13上午11:15:25
*/
public class PersonService implements IPersonService {
private IPerson person;

public IPerson getPerson() {
return person;
}

/**
* 这个注释就是说明需要依赖注入
* @param person
*/
@Inject
public void setPerson(IPerson person) {
this.person = person;
}

public void say() {
System.out.println("userperson");
person.say();
}

}
====================================

package xworktest;

import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.ContainerBuilder;

/**
* 测试xwork的inject注解的使用。了解xwork的Container容器。
* 在xwork中的container使用了动态代理技术。
* @author wangmingjie
* @date 2008-9-13上午11:17:35
*/
public class ContainerTest {
private Container container;

public static void main(String[] args) {
ContainerTest test = new ContainerTest();
ContainerBuilder cb = new ContainerBuilder();
cb=cb.factory(IPerson.class,Person.class);
cb=cb.factory(IPersonService.class,PersonService.class);
test.setContainer(cb.create(true));
// 注释掉的代码也是正确的
//test.setContainer(new ContainerBuilder().factory(IPerson.class,
//Person.class).factory(IPersonService.class, PersonService.class)
//.create(true));

Container container = test.getContainer();

IPersonService person = container.getInstance(IPersonService.class);
person.say();

}

public Container getContainer() {
return container;
}

public void setContainer(Container container) {
this.container = container;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值