本篇博客记录学习之余的收获,基于spring5框架学习如何给IOC容器中注入bean
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>spring-annotationartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.12.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
dependencies>
project>
package com.ricardo.pojo;
/**
* @author ldy
* @date 2020-12-29 19:00
**/
public class Book {
private String name;
private Double price;
public Book() {
}
public Book(String name, Double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
以上我们的所需要用到的素材都准备好了
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="book" class="com.ricardo.pojo.Book">
<property name="name" value="java入门到入土">property>
<property name="price" value="99.99">property>
bean>
beans>
这里给com.ricardo.pojo.Book这个bean中的name和price属性分别赋予了两个值并且将使用标签将它注册到spring的IOC容器中
package com.ricardo.test;
import com.ricardo.config.SpringAnnotationConfig;
import com.ricardo.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
/**
* @author ldy
* @date 2020-12-29 19:03
**/
public class AnnotationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 使用bean标签中的id获取
Book book = (Book) applicationContext.getBean("book");
System.out.println(book);
}
}
package com.ricardo.test;
import com.ricardo.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author ldy
* @date 2020-12-29 19:03
**/
public class AnnotationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 使用bean标签中的id获取
Book book = applicationContext.getBean(Book.class);
System.out.println(book);
}
}
可以成功的看到,使用xml配置文件的方式将bean注册到IOC容器中是没有问题的
package com.ricardo.config;
import com.ricardo.pojo.Book;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author ldy
* @date 2020-12-29 18:58
**/
// 配置类==配置文件
@Configuration // 告诉spring这是一个配置类
public class SpringAnnotationConfig {
// 注册组件 等同于bean标签 给IOC容器中注册一个bean;类型为返回值的类型,id默认是用方法名作为id
@Bean
public Book getBook(){
return new Book("Python天下第一",999.99D);
}
}
package com.ricardo.test;
import com.ricardo.config.SpringAnnotationConfig;
import com.ricardo.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author ldy
* @date 2020-12-29 19:03
**/
public class AnnotationTest {
public static void main(String[] args) {
// 和ClassPathXmlApplicationContext不同的是最后括号中一个是用xml的名称,一个使用配置类的类型
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringAnnotationConfig.class);
// 使用类型获取,同样也可以使用id获取
// Book book = applicationContext.getBean("book");
Book book = applicationContext.getBean(Book.class);
System.out.println(book);
}
}
package com.ricardo.test;
import com.ricardo.config.SpringAnnotationConfig;
import com.ricardo.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author ldy
* @date 2020-12-29 19:03
**/
public class AnnotationTest {
public static void main(String[] args) {
// 和ClassPathXmlApplicationContext不同的是最后括号中一个是用xml的名称,一个使用配置类的类型
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringAnnotationConfig.class);
// 使用类型获取,同样也可以使用id获取
// Book book = applicationContext.getBean("book");
Book book = applicationContext.getBean(Book.class);
System.out.println(book);
String[] beanNamesForType = applicationContext.getBeanNamesForType(Book.class);
for (String names : beanNamesForType) {
System.out.println(names);
}
}
}
package com.ricardo.config;
import com.ricardo.pojo.Book;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author ldy
* @date 2020-12-29 18:58
**/
// 配置类==配置文件
@Configuration // 告诉spring这是一个配置类
public class SpringAnnotationConfig {
// 注册组件 等同于bean标签 给IOC容器中注册一个bean;类型为返回值的类型,id默认是用方法名作为id
@Bean(value = "book")
public Book getBook(){
return new Book("Python天下第一",999.99D);
}
}
在bean注解后面括号中手动给他赋予名称
尚硅谷Spring注解驱动教程(雷丰阳源码级讲解)