Spring学习笔记(九)常用注解

常用注解

使用注解前提

  1. 导入JAR包
  2. 用注解配置管理资源
  3. 创建 spring 的 的 xml 配置 文件
<?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">
<!-- 告知 spring 创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>

用于创建对象的注解:<bean id="" class="">

  1. @Component
    • 作用:把资源让 spring 来管理。相当于在 xml 中配置一个 bean。
    • 属性:
      • value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名。首字母小写。
  2. @Controller @Service @Repository
    • 三个注解都是针对一个@Component的衍生注解,他们的作用及属性都是一模一样的。
    • 提供了更加明确的语义化。
      • @Controller :一般用于表现层的注解。
      • @Service :一般用于业务层的注解。
      • @Repository :一般用于持久层的注解。
    • 细节:如果注解中有且只有一个属性要赋值时 ,且名称是 value ,value 在赋值是可以不写。

用于注入数据的:<property name="" ref=""> <property name="" value="">

  1. @Autowired
    • 作用:
      • 自动按照类型注入。当使用注解注入属性时,set方法可以省略。
      • 它只能注入其他 bean 类型。
      • 当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到就报错。
  2. @Qualifier
    • 作用:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用;但是给方法参数注入时,可以独立使用。
    • 属性:value:指定 bean 的 id。
  3. @Resource
    • 作用:直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
    • 属性:name:指定 bean 的 id。
  4. @Value
    • 作用:注入基本数据类型和 String 类型数据的
    • 属性:value:用于指定值

用于改变作用范围的:scope=""

  1. @Scope
    • 作用:指定 bean 的作用范围。
    • 属性:value:指定范围的值。:singleton prototype request session globalsession

和生命周期相关的:init-method="" destroy-method=""

  1. @PostConstruct:用于指定初始化方法。
  2. @PreDestroy:用于指定销毁方法。

新注解

@Configuration

  1. 作用:用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。
  2. 属性:value:用于指定配置类的字节码

@ComponentScan

  1. 作用:
    • 用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package="com.lwb"/>是一样的。
  2. 属性:
    • basePackages:用于指定要扫描的包。和该注解中的 value 属性作用一样。(如果是多个包的话,需要加大括号)
  3. 实例
@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
}

@Bean

  1. 作用:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
  2. 属性:name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)。
  3. 实例:
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
	<property name="user" value="root"></property>
	<property name="password" value="1234"></property>
</bean>
  • 相对应的注解
@Bean(name="dataSource")
public DataSource createDataSource() {
	try {
		ComboPooledDataSource ds = new ComboPooledDataSource();
		ds.setUser("root");
		ds.setPassword("1234");
		ds.setDriverClass("com.mysql.jdbc.Driver");
		ds.setJdbcUrl("jdbc:mysql:///spring");
		return ds;
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}

@Import

  1. 作用:用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问题。
  2. 属性:value[]:用于指定其他配置类的字节码。
@Import({ JdbcConfig.class})
public class SpringConfiguration {
}
public class JdbcConfig{
}

@PropertySource

  1. 作用:用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
  2. 属性:value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:
  3. 实例:
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig{
}
jdbc.properties 文件:
	jdbc.driver=com.mysql.jdbc.Driver
	jdbc.url=jdbc:mysql://localhost:3306/day44_ee247_spring
	jdbc.username=root
	jdbc.password=1234

猜你喜欢

转载自blog.csdn.net/qq_41816516/article/details/106909611