说明 :IOC注解部分:参考网上课程,自己写的总结,如有侵权,联系删除。
目录
@Repository、@Service、@Controller
注解实现IOC_开始
注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只 是配置的形式不一样。
- 创建一个新的Spring项目。
- 编写pojo,dao,service类。
- 编写空的配置文件,如果想让该文件支持注解,需要添加新的约束:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/context/spring-context.xsd"> </beans>
@Component
作用:用于创建对象,放入Spring容器,相当于<bean id="" class="">
位置:类上方
注意:
要在配置文件中配置扫描的包,扫描到该注解才能生效。
<context:component-scan base-package="com.dream"></context:component-scan>
@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
// 此时bean的id为studentDaoImpl @Component public class StudentDaoImpl implements StudentDao{ }
// 此时bean的id为studentDao //@Component(value = "studentDao") @Component("studentDao") //简写 public class StudentDaoImpl implements StudentDao{ }
@Repository、@Service、@Controller
作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。
位置:
- @Repository用于Dao层
- @Service用于Service层
- @Controller用于Controller层
@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}
@Scope
作用:指定bean的创建策略
位置:类上方
取值:singleton prototype request session globalsession
@Service
@Scope("singleton")
public class StudentService {}
@Autowired
作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替<bean>中的依赖注入配置。
位置:属性上方、setter方法上方、构造方法上方。
注意:
- @Autowired 写在属性上方进行依赖注入时,可以省略setter方法。
- 容器中没有对应类型的对象会报错
- 容器中有多个对象匹配类型时,会找beanId等于属性名的对 象,找不到会报错。
@Qualifier
作用:在按照类型注入对象的基础上,再按照bean的id注入。
位置:属性上方
注意:@Qualifier必须和@Autowired一起使用。
@Component
public class StudentService {
@Autowired
@Qualifier("studentDaoImpl2")
private StudentDao studentDao;
}
@Value
作用:注入String类型和基本数据类型的属性值。
位置:属性上方
- 直接设置固定的属性值
@Service public class StudentService { @Value("10") private int count; @Value("hello") private String str; }
-
获取配置文件中的属性值:
-
编写配置文件db.properties
jdbc.username=root jdbc.password=123456
-
spring核心配置文件扫描配置文件
<context:property-placeholder location="db.properties"></context:property-placeholder>
-
注入配置文件中的属性值
@Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password;
-
@Configuration
此时基于注解的IOC配置已经完成,但是我们依然离不开Spring的xml配置文件。接下来我们脱离bean.xml,使用纯注解实现IOC。
在真实开发中,我们一般还是会保留xml配置文件,很多情况下 使用配置文件更加方便。
纯注解实现IOC需要一个Java类代替xml文件。这个Java类上方需要 添加@Configuration,表示该类是一个配置类,作用是代替配置文件。
@Configuration
public class SpringConfig {
}
@ComponentScan
作用:指定spring在初始化容器时扫描的包。
位置:配置类上方
@Configuration
@ComponentScan("com.dream")
public class SpringConfig {
}
@PropertySource
作用:代替配置文件中的 扫描配置文件
位置:配置类上方
注意:配置文件位置前要加关键字 classpath
@Configuration
@PropertySource("classpath:db.properties")
public class JdbcConfig {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
}
@Bean
作用:将方法的返回值对象放入Spring容器中。如果想将第三方类 的对象放入容器,可以使用@Bean
位置:配置类的方法上方(emm 好像别的能被扫描的类方法上也可以)。
属性:name:给bean对象设置id
注意:@Bean修饰的方法如果有参数,spring会根据参数类型从容 器中查找可用对象。
举例:如果想将jdbc连接对象放入Spring容器,我们无法修改 Connection源码添加@Component,此时就需要使用将@Bean该 对象放入Spring容器
- 添加驱动依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency>
- 设定的配置类中定义方法将Connection对象放入Spring容器
@Bean(name = "connection") public Connection getConnection(){ try { Class.forName("com.mysql.cj.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql:///mysql", "root", "123456"); } catch (Exception exception) { return null; } }
- 使用ac测试
@Test public void t5(){ ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class); Connection connection = (Connection)ac.getBean("connection"); System.out.println(connection); }
@Import
作用:如果配置过多,会有多个配置类,该注解可以为主配置类导 入其他配置类
位置:主配置类上方
// Jdbc配置类
@Configuration
public class JdbcConfig {
@Bean(name = "connection")
public Connection getConnection(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql:///mysql", "root", "123456");
} catch (Exception exception) {
return null;
}
}
}
// 主配置类
@Configuration
@ComponentScan("com.dream")
@Import(JdbcConfig.class) //({JdbcConfig.class,..,..,..}) 可以写多个
public class SpringConfig {
}
暂时ioc部分的注解结束