方式一:@Component + @ComponentScan + @Value + @Autowired
首先还是pom文件,maven项目依赖必不可少。
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.2.5.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.18.20</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.annotation</groupId> | |
<artifactId>javax.annotation-api</artifactId> | |
<version>1.3.2</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> |
然后,写两个Java Bean,一个是Student学生类、另一个是School学校类。
由于不写xml配置,所以在两个类上方都要加上 @Component 注解,通过注解的方式将其交给Spring IOC容器管理,@Value注解则用于给8种基本数据类型以及String类型做依赖注入,@Autowired是针对引用类型的,这里不再多说了。
package com.szh.bean; | |
import lombok.Data; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Component; | |
/** | |
* | |
*/ | |
public class Student { | |
private String name; | |
private Integer age; | |
private School school; | |
} | |
package com.szh.bean; | |
import lombok.Data; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Component; | |
/** | |
* | |
*/ | |
public class School { | |
private String name; | |
private String address; | |
} |
下面要写一个配置类,功能就是添加包扫描机制,确保上面那两个@Component 注解修饰的Java Bean可以被Spring扫描并添加至容器中。
package com.szh.config; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
/** | |
* | |
*/ | |
public class SpringConfig { | |
} |
最后是我们的测试类了。
package com.szh; | |
import com.szh.bean.Student; | |
import com.szh.config.SpringConfig; | |
import org.junit.Test; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
/** | |
* | |
*/ | |
public class MyTest { | |
public void testIocAnnotation() { | |
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class); | |
Student student = (Student) ioc.getBean("student"); | |
System.out.println(student); | |
} | |
} |
方式二:@Configuration + @Bean
pom文件和方式一是一样的。
下面是不一样的Java Bean。
package com.szh.entity; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
/** | |
* | |
*/ | |
public class Goods { | |
private String name; | |
private String info; | |
} | |
package com.szh.entity; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import java.math.BigDecimal; | |
/** | |
* | |
*/ | |
public class Order { | |
private Integer id; | |
private BigDecimal totalFee; | |
private Goods goods; | |
} |
然后是该方式对应的配置类,采用@Bean实现。
package com.szh.config; | |
import com.szh.entity.Goods; | |
import com.szh.entity.Order; | |
import org.springframework.beans.factory.annotation.Autowire; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import java.math.BigDecimal; | |
/** | |
* | |
*/ | |
public class SpringConfig2 { | |
public Goods goods() { | |
return new Goods("联想-拯救者", "一款不错的游戏笔记本"); | |
} | |
public Order order(Goods goods) { | |
return new Order(1, new BigDecimal(9999), goods); | |
} | |
} |
最后是这种方式的测试类代码。
@Test | |
public void testIocAnnotation2() { | |
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig2.class); | |
Order order = (Order) ioc.getBean("order"); | |
System.out.println(order); | |
System.out.println("IOC容器中存在的bean如下:"); | |
String[] beanDefinitionNames = ioc.getBeanDefinitionNames(); | |
for (String bean : beanDefinitionNames) { | |
System.out.println(bean); | |
} | |
} |