- 什么是Bean管理
Bean管理指的是两个操作 (1) Spring创建对象 (2) Spirnq注入属性 - Bean管理操作有两种方式
(1) 基于xml配置文件方式实现 (2) 基于注解方式实现
IOC操作Bean管理(基于注解方式)
1、什么是注解 ?(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…) ?(2)使用注解,注解作用在类上面,方法上面, 属性上面 ?(3)使用注解目的:简化xml配置
2、Spring针对Bean管理中创建对象提供注解 ?(1) @Component ?(2) @Service ?(3) @Controller ?(4) @Repository 上面四个注解功能是一样的,都可以用来创建bean实例
基于注解方式实现对象创建
(1)引入依赖 (2)开启组件扫描
- 如果扫描多个包,多个包使用逗号隔开
- 扫描包上层目录
bean1.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd">
<context:component-scan base-package="com.atguigu.spring5"></context:component-scan>
</beans>
(3)创建类,在类上面添加创建对象注解
UserService.java
package com.atguigu.spring5.service;
import org.springframework.stereotype.Component;
@Component(value = "userService")
public class UserService {
public void add() {
System.out.println("service add...");
}
}
测试
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
测试结果
组件扫描配置
<context:component-scan base-package="com.atguigu"use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframnework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
基于注解方式实现属性注入
(1)@Autowired: 根据属性类型进行自动装配 1、把service和 dao对象创建,在service和dao类添加创建对象注解 UserDao.java
package com.atguigu.spring5.dao;
public interface UserDao {
public void add();
}
UserDaoImpl.java
package com.atguigu.spring5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("dao.add");
}
}
2、在service注入dao对象,在service类添加dao,类型属性,在属性上面使用注解 UserService.java
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void add() {
System.out.println("service add...");
userDao.add();
}
}
(2)@Qualifier: 根据属性名称进行注入 这个@Qualifier注解的使用,和上面@Autowired一起使用 用途:一个接口可能有多个实现类对象,@Qualifier注解可以指定实现类对象
@Autowired
@Qualifier(value = "userDaoImpl1")
private UserDao userDao;
(3)@Resource: 可以根据类型注入,可以根据名称注入
@Resource
@Resource(name="userDaoImpl1")
private UserDao userDao;
(4)@Value: 注入普通类型属性
@Value(value = "abc")
private String name;
完全注解开发
SpringConfig.java
package com.atguigu.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {
}
Test
@Test
public void testService1() {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
|