package com.xiaohui.myinject2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDaoImpl {
private DbUtil util;
@Autowired
public void setUtil(DbUtil util) {
this.util = util;
}
public void save(){
System.out.println("save in PersonDaoImpl....");
util.save();
}
}
PersonServiceImpl类:
package com.xiaohui.myinject2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl {
private PersonDaoImpl dao;
@Autowired
public void setDao(PersonDaoImpl dao) {
this.dao = dao;
}
public void save(){
System.out.println("PersonServiceImpl save user.....");
dao.save();
}
}
PersonActionPersonActionPersonAction类:
package com.xiaohui.myinject2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class PersonAction {
private PersonServiceImpl service;
@Autowired
public void setService(PersonServiceImpl service) {
System.out.println("save in action.....");
this.service = service;
}
public void execute(){
service.save();
}
}
测试类:
package com.xiaohui.app;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xiaohui.myinject2.PersonAction;
public class AppTest {
@Test
public void testSpring() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
PersonAction service = ctx.getBean(PersonAction.class);
service.execute();
}
}
打印结果: save in action.....
save in PersonDaoImpl....
save object in dbutil....