注入bean相关的一些注解
1、@Repository
DAO层注解,DAO层中接口继承JpaRepository<T,ID extends Serializable>,需要在build.gradle中引入相关jpa的一个jar自动加载。 Repository注解源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
2、@Service
1、 @Service是@Component注解的一个特例,作用在类上 @Service注解作用域默认为单例
2、使用注解配置和类路径扫描时,被@Service注解标注的类会被Spring扫描并注册为Bean
3、@Service用于标注服务层组件,表示定义一个bean @Service使用时没有传参数,Bean名称默认为当前类的类名,首字母小写
4、@Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用时传参数,使用value作为Bean名字
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
3、@Scope作用域注解
1、@Scope作用在类上和方法上,用来配置 spring bean 的作用域,它标识 bean 的作用域
2、属性介绍
value
singleton 表示该bean是单例的。(默认)
prototype 表示该bean是多例的,即每次使用该bean时都会新建一个对象。
request 在一次http请求中,一个bean对应一个实例。
session 在一个httpSession中,一个bean对应一个实例。
proxyMode
DEFAULT 不使用代理。(默认)
NO 不使用代理,等价于DEFAULT。
INTERFACES 使用基于接口的代理(jdk dynamic proxy)。
TARGET_CLASS 使用基于类的代理(cglib)。
@Scope源码:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
/**
* Alias for {@link #scopeName}.
* @see #scopeName
*/
@AliasFor("scopeName")
String value() default "";
@AliasFor("value")
String scopeName() default "";
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
}
4、@Entity实体类注解
@Table(name =“数据库表名”),这个注解也注释在实体类上,对应数据库中相应的表。
@Id、@Column注解用于标注实体类中的字段,pk字段标注为@Id,其余@Column。
CREATE TABLE `user_entity` (
`id` int(11) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
package sun.rain.amazing.javax.anno.domain;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author sunRainAmazing
*/
@Entity
@Data
public class UserEntity {
/**
* No identifier specified for entity: sun.rain.amazing.javax.anno.domain.UserEntity
*/
@Id
private int id;
private String username;
private String email;
public UserEntity(String username, String email) {
this.username = username;
this.email = email;
}
public UserEntity() {
}
}
CREATE TABLE `ue_pro` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`mobile` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
package sun.rain.amazing.javax.anno.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author sunRainAmazing
*/
@Data
@AllArgsConstructor
@Entity(name="UePro")
public class UserEntityProperty {
/**
* No identifier specified for entity: sun.rain.amazing.javax.anno.domain.UserEntity
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String email;
private String mobile;
public UserEntityProperty(String username, String email, String mobile) {
this.username = username;
this.email = email;
this.mobile = mobile;
}
public UserEntityProperty() {
}
}
5、@Bean产生一个bean的方法
@Bean明确地指示了一种方法,产生一个bean的方法,并且交给Spring容器管理。支持别名@Bean(“xx-name”)
class A{
@Bean
public AccountDao accountDao(){
return new AccountDao();
}
}
6、@Autowired 自动导入
@Autowired注解作用在构造函数、方法、方法参数、类字段以及注解上
@Autowired注解可以实现Bean的自动注入
7、@Component
把普通pojo实例化到spring容器中,相当于配置文件中的xml配置文件
虽然有了@Autowired,但是我们还是要写一堆bean的配置文件,相当麻烦,而@Component就是告诉spring,我是pojo类,把我注册到容器中吧,spring会自动提取相关信息。那么我们就不用写麻烦的xml配置文件了