Spring IoC
Inversion of Control
控制反转
主要靠依赖注入实现
IoC容器是实现依赖注入的关键
bean调用
示例:
在test中添加注解:@ContextConfiguration(classes = CommnunityApplication.class) //启用CommunityApplication作为配置类
创建dao目录
package com.newcoder.community.dao;
public interface AlphaDao {
public String select();
}
package com.newcoder.community.dao;
import org.springframework.stereotype.Repository;
@Repository //让容器能扫描到
public class AlphaDaoHibernateImpel implements AlphaDao{
@Override
public String select() {
return "Hibernate";
}
}
test类中
package com.newcoder.community;
import com.newcoder.community.dao.AlphaDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
@SpringBootTest
@ContextConfiguration(classes = CommnunityApplication.class) //启用CommunityApplication作为配置类
class CommnunityApplicationTests implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Test
public void testApplicationContext() {
System.out.println(applicationContext);
AlphaDao alphaDao = applicationContext.getBean(AlphaDao.class);
System.out.println(alphaDao.select());
}
}
运行测试程序
当有两个类实现同一个接口,需要再使用的类添加@Primary注解
package com.newcoder.community.dao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
@Repository
@Primary
public class AlphaMybatisImpel implements AlphaDao{
@Override
public String select() {
return "Mybatis";
}
}
或者修改bean名字
@Repository("alphaHibernate")
alphaDao = applicationContext.getBean("alphaHibernate",AlphaDao.class);
System.out.println(alphaDao.select());
bean的初始化与销毁
创建Service包
package com.newcoder.community.service;
import com.newcoder.community.dao.AlphaDao;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.security.PublicKey;
@Service
public class AlphaService {
public AlphaService() {
System.out.println("实例化AlphaService");
}
@PostConstruct
public void init() {
System.out.println("初始化AlphaService");
}
@PreDestroy
public void destroy() {
System.out.println("销毁AlphaService");
}
}
单独写一个测试方法
@Test
public void testBeanManagement() {
AlphaService alphaService = applicationContext.getBean(AlphaService.class);
System.out.println(alphaService);
}
运行
注意:默认为单例模式(singleton) 若要改成多例模式需要加入在Service@scope(“prototype”)注解
配置第三方Bean
创建Config包
package com.newcoder.community.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat;
@Configuration
public class AlphaConfig {
@Bean
public SimpleDateFormat singleDateFormat() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}
再写一个测试方法
@Test
public void testBeanConfig() {
SimpleDateFormat simpleDateFormat = applicationContext.getBean(SimpleDateFormat.class);
System.out.println(simpleDateFormat.format(new Date()));
}
运行
依赖注入
@Autowired
@Qualifier("alphaHibernate")
private AlphaDao alphaDao;
@Autowired
private AlphaService alphaService;
@Autowired
private SimpleDateFormat simpleDateFormat;
@Test
public void testDI() {
System.out.println(alphaDao);
System.out.println(alphaService);
System.out.println(simpleDateFormat);
}
依赖注入整合
浏览器通过controller发球请求,service处理业务,dao与数据库交互返回数据
service中注入dao,通过find方法获取dao的数据
@Autowired
private AlphaDao alphaDao;
public String find() {
return alphaDao.select();
}
在controller中获取service返回的数据
@Autowired
private AlphaService alphaService;
@RequestMapping("/data")
@ResponseBody
public String getData() {
return alphaService.find();
}
打开http://localhost:8081/community/alpha/data