1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:
org.springframework
spring-context
5.2.0.RELEASE
junit
junit
4.12
test
2、创建dao层、service层、controller层,代码如下:
1)dao层代码:
package com.rf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
}
2)service层代码:
package com.rf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rf.dao.BookDao;
@Service
public class BookService {
@Autowired
BookDao bookDao;
public void print(){
System.out.println(bookDao);
}
@Override
public String toString() {
return "BookService [bookDao=" + bookDao + "]";
}
}
3)controller层代码:
package com.rf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.rf.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
}
3、编写配置类,代码如下:
package com.rf.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 自动装配:
* Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
* 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
}
4、编写测试类,代码如下:
package com.rf.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;
public class Test2 {
@org.junit.Test
public void test(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
System.out.println("ioc容器创建完成了。。。。");
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
BookDao bookDao = ctx.getBean(BookDao.class);
System.out.println(bookDao);
}
}
5、启动测试类,运行效果如下:
1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:
org.springframework
spring-context
5.2.0.RELEASE
junit
junit
4.12
test
2、创建dao层、service层、controller层,代码如下:
1)dao层代码:
package com.rf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
private String lable="1";
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
@Override
public String toString() {
return "BookDao [lable=" + lable + "]";
}
}
2)service层代码:
package com.rf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rf.dao.BookDao;
@Service
public class BookService {
@Autowired
BookDao bookDao;
public void print(){
System.out.println(bookDao);
}
@Override
public String toString() {
return "BookService [bookDao=" + bookDao + "]";
}
}
3)controller层代码:
package com.rf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.rf.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
}
3、编写配置类,代码如下:
package com.rf.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 自动装配:
* Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
* 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
//通过@Bean的方式在注册一个bookDao组件
@Bean("bookDao2")
public BookDao bookDao(){
BookDao bookDao = new BookDao();
bookDao.setLable("2");
return bookDao;
}
}
4、编写测试类,代码如下:
package com.rf.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;
public class Test2 {
@org.junit.Test
public void test(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
System.out.println("ioc容器创建完成了。。。。");
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
5、启动测试类,运行效果如下:
1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:
org.springframework
spring-context
5.2.0.RELEASE
junit
junit
4.12
test
2、创建dao层、service层、controller层,代码如下:
1)dao层代码:
package com.rf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
private String lable="1";
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
@Override
public String toString() {
return "BookDao [lable=" + lable + "]";
}
}
2)service层代码:
package com.rf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rf.dao.BookDao;
@Service
public class BookService {
@Autowired
BookDao bookDao2;
public void print(){
System.out.println(bookDao2);
}
@Override
public String toString() {
return "BookService [bookDao2=" + bookDao2 + "]";
}
}
3)controller层代码:
package com.rf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.rf.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
}
3、编写配置类,代码如下:
package com.rf.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 自动装配:
* Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
* 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
//通过@Bean的方式在注册一个bookDao组件
@Bean("bookDao2")
public BookDao bookDao(){
BookDao bookDao = new BookDao();
bookDao.setLable("2");
return bookDao;
}
}
4、编写测试类,代码如下:
package com.rf.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;
public class Test2 {
@org.junit.Test
public void test(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
System.out.println("ioc容器创建完成了。。。。");
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
5、启动测试类,运行效果如下:
1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:
org.springframework
spring-context
5.2.0.RELEASE
junit
junit
4.12
test
2、创建dao层、service层、controller层,代码如下:
1)dao层代码:
package com.rf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
private String lable="1";
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
@Override
public String toString() {
return "BookDao [lable=" + lable + "]";
}
}
2)service层代码:
package com.rf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rf.dao.BookDao;
@Service
public class BookService {
@Qualifier("bookDao")//使用@Qualifier指定需要装配的组件的id,而不是使用属性名
@Autowired
BookDao bookDao2;
public void print(){
System.out.println(bookDao2);
}
@Override
public String toString() {
return "BookService [bookDao2=" + bookDao2 + "]";
}
}
3)controller层代码:
package com.rf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.rf.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
}
3、编写配置类,代码如下:
package com.rf.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 自动装配:
* Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
* 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
//通过@Bean的方式在注册一个bookDao组件
@Bean("bookDao2")
public BookDao bookDao(){
BookDao bookDao = new BookDao();
bookDao.setLable("2");
return bookDao;
}
}
4、编写测试类,代码如下:
package com.rf.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;
public class Test2 {
@org.junit.Test
public void test(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
System.out.println("ioc容器创建完成了。。。。");
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
5、启动测试类,运行效果如下:
1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:
org.springframework
spring-context
5.2.0.RELEASE
junit
junit
4.12
test
2、创建dao层、service层、controller层,代码如下:
1)dao层代码:
package com.rf.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
private String lable="1";
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
@Override
public String toString() {
return "BookDao [lable=" + lable + "]";
}
}
2)service层代码:
package com.rf.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rf.dao.BookDao;
@Service
public class BookService {
@Autowired
BookDao bookDao;
public void print(){
System.out.println(bookDao);
}
@Override
public String toString() {
return "BookService [bookDao=" + bookDao + "]";
}
}
3)controller层代码:
package com.rf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.rf.service.BookService;
@Controller
public class BookController {
@Autowired
BookService bookService;
}
3、编写配置类,代码如下:
package com.rf.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 自动装配:
* Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
* 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
//通过@Bean的方式在注册一个bookDao组件
@Primary //让Spring进行自动装配的时候,默认使用首选的bean;
@Bean("bookDao2")
public BookDao bookDao(){
BookDao bookDao = new BookDao();
bookDao.setLable("2");
return bookDao;
}
}
4、编写测试类,代码如下:
package com.rf.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;
public class Test2 {
@org.junit.Test
public void test(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
System.out.println("ioc容器创建完成了。。。。");
BookService bookService = ctx.getBean(BookService.class);
System.out.println(bookService);
}
}
5、启动测试类,运行效果如下: