标签:nts context auto pack wired out can service style
@Service public class BookService { @Autowired private BookDao bookDao; public void print(){ System.out.println(bookDao); } }
applicationContext.getBean(BookDao.class);
applicationContext.getBean("bookDao");
package com.sunxiaping.dao; import org.springframework.stereotype.Repository; @Repository public class BookDao { }
package com.sunxiaping.serivice; import com.sunxiaping.dao.BookDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class BookService { @Autowired private BookDao bookDao; public void print(){ System.out.println(bookDao); } }
package com.sunxiaping.controller; import com.sunxiaping.serivice.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class BookController { @Autowired private BookService bookService; public void print(){ System.out.println(bookService); } }
package com.sunxiaping.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan(value = "com.sunxiaping") @Configuration //告诉Spring这是一个配置类 public class SpringConfig { }
package com.sunxiaping.test; import com.sunxiaping.config.SpringConfig; import com.sunxiaping.controller.BookController; import com.sunxiaping.serivice.BookService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); BookController bookController = context.getBean(BookController.class); bookController.print(); BookService bookService = context.getBean(BookService.class); bookService.print(); } }
标签:nts context auto pack wired out can service style
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/11946403.html