1、IOC 操作 Bean 管理(基于注解方式)
- 什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置 - Spring 针对 Bean 管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
- 上面四个注解功能是一样的,都可以用来创建 bean 实例
2、基于注解方式实现对象创建
-
引入依赖
-
创建一个 UserSrevice 类
package com.su.spring5.service;
import com.su.spring5.dao.UserDao;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
// @Service
public class UserService {
public void add() {
System.out.println("service add...........");
}
}
- 编写配置文件
<!--开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.su"></context:component-scan>
- 测试
@Test
public void testService1() {
ApplicationContext context
= new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
3、基于注解方式实现属性注入
@Autowired:根据属性类型进行自动装配
@Qualifier:根据名称进行注入(使用时要配合 @Autowired )
@Resource:可以根据类型注入,可以根据名称注入
@Value:注入普通类型属性
- 创建UserService 、UserDao、UserDaoImpl 三个类
package com.su.spring5.service;
import com.su.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
//在注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
// @Service
public class UserService {
@Value(value = "abc")
private String name;
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired //根据类型进行注入
// @Qualifier(value = "userDaoImpl1") //根据名称进行注入
private UserDao userDao;
// //@Resource //根据类型进行注入
// @Resource(name = "userDaoImpl1") //根据名称进行注入
// private UserDao userDao;
public void add() {
System.out.println("service add......."+name);
userDao.add();
}
}
package com.su.spring5.dao;
public interface UserDao {
public void add();
}
package com.su.spring5.dao;
import org.springframework.stereotype.Component;
@Component(value = "userDao1")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add.....");
}
}
- 编写配置程序
<!--开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.su"></context:component-scan>
- 测试
@Test
public void testService1() {
ApplicationContext context
= new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
4、完全注解开发
- 编写配置类 SpringConfig
package com.su.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = "com.su")//扫描
public class SpringConfig {
}
- 测试
使用 AnnotationConfigApplicationContext 函数
@Test
public void testService2() {
ApplicationContext context
= new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}