是Spring4.x推荐的配置方式 但我觉得一点用都没。大概逻辑如下 :
package com.bihu.conno.helloDao; public interface HelloDao { void show(); }HelloDao
package com.bihu.conno.helloDaoImpl; import com.bihu.conno.helloDao.HelloDao; public class HelloDaoImpl implements HelloDao { public HelloDaoImpl() { } @Override public void show() { System.out.println("Dao -- Show"); } }HelloDaoImpl
package com.bihu.conno.helloService; public interface HelloService { void show(); }HelloService
package com.bihu.conno.HelloServiceImpl; import com.bihu.conno.helloDao.HelloDao; import com.bihu.conno.helloService.HelloService; public class HelloServiceImpl implements HelloService { //HelloDao HelloDao dao; //构造 public HelloServiceImpl(HelloDao helloDao) { this.dao = helloDao; } @Override public void show() { dao.show(); //调用Dao的Show System.out.println("Service -- Show"); } }HelloServiceImpl
package com.bihu.conno.Controller; import com.bihu.conno.helloService.HelloService; public class HelloController { //HelloService HelloService service; //构造 public HelloController(HelloService service) { this.service = service; } public void show() { service.show(); } }HelloController
package com.bihu.conno; import com.bihu.conno.Controller.HelloController; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Config.class); HelloController bean = app.getBean(HelloController.class); bean.show(); } }Config配置类
package com.bihu.conno; import com.bihu.conno.Controller.HelloController; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Config.class); HelloController bean = app.getBean(HelloController.class); bean.show(); } }Test测试类
运行: