springboot注解直接用,不用什么操作
spring使用注解:在spring4之后,要使用注解必须确保aop的包被导入了,要使用注解还要做如下配置
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!--扫描com.wt.pojo包下的类,这个包下的component注解就会生效-->
<context:component-scan base-package="com.wt.pojo"/>
<!--下面的bean被@Component注解取代了,bean中的property被@Value取代,scope被@Scope注解取代-->
<!-- <bean id="user" class="com.wt.pojo.User" scope="singleton">-->
<!-- <property name="name" value="wt"/>-->
<!-- </bean>-->
</beans>
@Autowired(实现自动装配)
@Autowired会在beans.xml中寻找是否与cat相同类型(Cat类型)的bean,既@Autowired是ByType的,然后实现自动装配,帮你实现<property name=“cat” ref=“与cat相同类型(就是Cat类型)的bean的id/>”,自己尝试时发现,如果在xml中注册了cat,cat1,都是Cat类型,运行不会报错,而如果将注册cat,cat2,都是Cat类型,会报错(因为ByType),猜测是不是如果发现多个同type的,就找bean的id与people的属性名相同的。(实例代码https://blog.csdn.net/kitahiragawa/article/details/113108236)顺带一提,@Autowired不止可以写在属性上面,也可以写在方法上面,详情见官网
@Qualifier(实现自动装配)
@Qualifier是搭配@Autowired使用的,写在@Autowired注解下面,如果发现了多个类型相同的type,@Autowired不知装配哪一个,则可以使用@Qualifier来指定装配哪个id的bean,比如
@Autowired
@Qualifier(value="dog2")
private Dog dog;
实例代码https://blog.csdn.net/kitahiragawa/article/details/113108236)
@Resource(实现自动装配)
@Resource是java提供的注解,功能比较强大,@Resource默认按照ByName进行查找,找不到则使用ByType,如果发现了多个类型相同的type,会报错,@Resource也可以进行参数的设置
@Component(取代xml中的bean)
这个注解把下面的类变成bean,就不需要再xml文件中配置了,示例代码
package com.wt.pojo;
import org.springframework.stereotype.Component;
@Component//这个@Component注解相当于在配置文件中写了<bean id="user" class="com.wt.pojo.User"/>
public class User {
private String name="wt";
public String getName(){
return name;
}
}
@Value(取代bean中的property)
示例
package com.wt.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component//这个@Component注解相当于在配置文件中写了<bean id="user" class="com.wt.pojo.User"/>
public class User {
@Value("wt")//相当于<property name="name" value="wt"/>
private String name;
public String getName(){
return name;
}
}
@Repositroy | @Service | @Controller
这三个注解的功能和@Component一模一样,只是写法不同,用的地方也不同。在Web开发中,mvc三层架构,dao层用@Repositroy,controller层用@Controller,service层用@Service
@Scope(用来表示作用域)
示例
package com.wt.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component//这个@Component注解相当于在配置文件中写了<bean id="user" class="com.wt.pojo.User"/>
@Scope("singleton")//相当于<bean id="user" class="com.wt.pojo.User" scope="singleton"/>
public class User {
@Value("wt")//相当于<property name="name" value="wt"/>
private String name;
public String getName(){
return name;
}
}
@Configuration(用类替代xml配置文件)
使用这个注解就完全摒弃了xml配置文件,再利用 @Bean 注解来充当bean标签的作用。在springboot源码中可以看到大量的这个注解,其本质依然是 @Component
示例、下面的标注了@Configuration的类完全等价于下面的beans.xml
package com.wt.config;
import org.springframework.context.annotation.Configuration;
@Configuration//标注了这个,说明这个类是用来取代配置文件的
public class MyConfig {
@Bean
public User user01(){//这里的方法名相当于beans.xml中的bean的id
return new User("张三",20);//注入通过构造器
}
@Bean("tom")//括号里可以起别名
public Cat tomcatPet(){//这里的方法名相当于beans.xml中的bean的id
return new Cat("tomcat");//注入通过构造器
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user01" class="com.wt.pojo.User">
<property name="name" value="zhangsan"/>
<property name="age" value="20"/>
</bean>
<bean id="tomcatPet" class="com.wt.pojo.Cat" name="tom">
<property name="name" value="tomcat"/>
</bean>
</beans>
@Import
@Import,顾名思义,其作用是导入bean(也可叫组件),其可以同时导入多个bean,被@Import的类会自动的被调用无参构造得到bean,然后这些bean会被放入IoC容器里面供使用,示例:
package com.wt.config;
import org.springframework.context.annotation.Configuration;
@Import({Dog.class,Bird.class})
@Configuration//标注了这个,说明这个类是用来取代配置文件的
public class MyConfig {
@Bean
public User user01(){//这里的方法名相当于beans.xml中的bean的id
return new User("张三",20);//注入通过构造器
}
@Bean("tom")//括号里可以起别名
public Cat tomcatPet(){//这里的方法名相当于beans.xml中的bean的id
return new Cat("tomcat");//注入通过构造器
}
}
@Condition 条件装配
写在bean上面或者配置类上面当满足某些条件时(条件会写在@Condition()的()里面),才装配某个bean或者才使某个配置类生效
@ImportResource
写在配置类上,导入配置文件,若将之前用spring开发时写好的配置文件全部改写成注解的形式比较麻烦就可以直接导入配置文件。@ImportResource会把配置文件中的bean全装入IoC容器中。示例(假设存在某个配置文件beans.xml)
package com.wt.config;
import org.springframework.context.annotation.Configuration;
@Configuration//标注了这个,说明这个类是用来取代配置文件的
@ImportResource("classpath:beans.xml")//引入beans.xml配置文件
public class MyConfig {
@Bean
public User user01(){//这里的方法名相当于beans.xml中的bean的id
return new User("张三",20);//注入通过构造器
}
@Bean("tom")//括号里可以起别名
public Cat tomcatPet(){//这里的方法名相当于beans.xml中的bean的id
return new Cat("tomcat");//注入通过构造器
}
}
@ConfigurationProperties
写在一个Bean上面,这个注解实现bean与springboot中的唯一配置文件application.properties
的绑定,用法看示例:
application.properties
配置文件:
car.brand=ford
car.price=100000
Bean:
@Component
@ConfigurationProperties(prefix="car")//prefix指的是前缀,用来判别类中的属性与哪些数据一一对应
//做了这样的绑定后,在注册这个bean的时候这些数据会注入
//这个只能配合@Component,如果类上没有@Component,就要配合@EnableConfigurationProperties使用了
public calss Car{
private String brand;
private int price;
constructors and gettersetter...
}
实现这种绑定还有另外一种方法,就是在配置类上加上@EnableConfigurationProperties,示例:
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(Car.calss)
//这个注解一是开启了Car的配置绑定功能(绑定的前缀需要在Car类上面用@ConfigurationProperties注解标出),二是将Car的bean注册到容器当中
public class MyConfig {
@Bean
public Car car01(){
return new Car();
}
@Bean
public User user01(){
return new User("张三",20);
}
@Bean("tom")//括号里可以起别名
public Cat tomcatPet(){
return new Cat("tomcat");
}
}
小总结
xml功能更加完善,注解写起来简单