Spring全家桶
- Spring Framework
- Spring Boot
- Spring Cloud
- Spring Cloud Data Flow
Spring Framework
- Spring Core
- Spring Data Access
- Transactions, Spring MyBatis
- Web Servlet
- Integration
- Emain, Scheduling, AMQP, Security
Spring IoC
- Inversion of Control
- Dependency Injection
- IoC Container
- IoC容器,是实现依赖注入的关键,本质上是一个工厂
四种类型注解
注解 |
说明 |
@Component |
组件注解,通用注解,被该注解描述的类将被IoC容器管理并实例化 |
@Controller |
语义注解,说明当前类是mvc应用中的控制器,用于接收用户的输入,调用Service的方法以及向客户端返回结果 |
@Service |
语义注解,说明当前类是Service业务服务类,用于实现功能的最核心代码,通常这种类都是以xxxservice结尾 |
@Repository |
语义注解,说明当前类用于业务持久层,通常描述对应的Dao类,负责与数据库实现增删改查的Dao类 |
Controller -> Service -> Dao
从Srping容器获取bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @SpringBootTest @ContextConfiguration(classes = CommunityApplication.class) class CommunityApplicationTests implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }
@Test public void testApplicationContext(){ AlphaDao alphaDao = applicationContext.getBean("AlphaHibernate", AlphaDao.class); System.out.println(alphaDao.select()); } }
|
Bean注入
1 2
| @Autowired private AlphaDao alphaDao;
|