Spring bean 注解方式注入

本文介绍了Spring框架中Bean的注解配置方法,包括如何在配置文件中引入命名空间、添加组件扫描等,并详细解释了@Controller、@Service、@Repository、@Component及@Autowired等注解的使用方式。
部署运行你感兴趣的模型镜像

对于Spring bean 注解装配 目前已经有大量的文章包括文档,现总结一下。


1. 在applicationContext中引入命名空间


对于命名空间,可以在eclipse中安装Spring IDE 插件,打开配置文件,,选择design视图,可以很方便的添加删除命名空间。


xmlns:context="http://www.springframework.org/schema/context"    
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-2.5.xsd  

2. 在applicationContext中添加

<context:component-scan base-package="写上你要扫描的包"/>

对于<context:annotation-config/> 配置可以看这个文章,annotation


3. Annotation

@Controller

@Controller("Bean的名称")

定义控制层

@Service

@Service("Bean的名称")

定义业务层

在使用这个注解时,一定不要引错了,否则很难找到注入失败的原因。特别是项目引入webservice的框架时。

@Repository

@Repository("Bean的名称")

定义DAO层

@Component

定义一般的Bean


@Resource

是JSR-250规范中的。不建议使用。


@Autowired

可以标记没有get、set方法的私有字段

可以标记构造函数

可以标记普通的方法


对于这个注解,可以标记没有set、get方法的私有字段。

但是不能滥用,对于字段,如果对外是可获取或者可修改的,一定要有相应的set、get方法。

不能为了使用这个注解,而不去分析该不该有set、get方法。


@Autowired(required=true)

如果Spring解析的时候,没有找到匹配的bean则抛出异常,默认为false



这篇文章写的比较详细,代码示例可以看这里,Spring bean 注解配置


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

SpringBean注入方式主要分为基于注解和基于 XML 两种类型,以下为你详细介绍: ### 使用注解注入 Bean - **使用注解(@Component、@Service、@Repository、@Controller)和 @Autowired**:`@Component` 是一个通用的注解,用于将类标记为 Spring 管理的 Bean。`@Service`、`@Repository` 和 `@Controller` 是 `@Component` 的特定形式,分别用于服务层、数据访问层和表现层。`@Autowired` 用于自动装配 Bean。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service class MyService { private MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } } ``` - **使用 @Configuration 和 @Bean**:`@Configuration` 用于标记一个类为配置类,`@Bean` 用于在配置类中定义 Bean。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } ``` - **使用构造函数注入**:通过构造函数将依赖的 Bean 注入到目标 Bean 中。 ```java class MyBean { private AnotherBean anotherBean; public MyBean(AnotherBean anotherBean) { this.anotherBean = anotherBean; } } ``` - **使用 @Value 注入属性值**:`@Value` 用于注入属性值,可以从配置文件或环境变量中获取。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component class MyComponent { @Value("${my.property}") private String myProperty; } ``` - **使用 @Primary 标注优先注入Bean**:当有多个候选 Bean 时,使用 `@Primary` 标注的 Bean 将被优先注入。 ```java import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @Primary class PrimaryBean implements MyInterface { // 实现接口方法 } ``` ### 基于 XML 注入 Bean - **构造器注入**:通过构造函数注入依赖的 Bean。 ```xml <bean id="myBean" class="com.example.MyBean"> <constructor-arg ref="anotherBean"/> </bean> ``` - **Setter 注入**:通过 setter 方法注入依赖的 Bean。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="anotherBean" ref="anotherBean"/> </bean> ``` - **接口注入(与 Spring 的 FactoryBean 结合使用)**:通过实现 `FactoryBean` 接口来创建 Bean。 ```java import org.springframework.beans.factory.FactoryBean; import org.springframework.stereotype.Component; @Component class MyFactoryBean implements FactoryBean<MyBean> { @Override public MyBean getObject() throws Exception { return new MyBean(); } @Override public Class<?> getObjectType() { return MyBean.class; } } ``` - **集合注入**:注入集合类型的依赖。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="myList"> <list> <ref bean="bean1"/> <ref bean="bean2"/> </list> </property> </bean> ``` - **自动装配(Autowiring)注入**:Spring 自动根据类型或名称进行 Bean 的装配。 ```xml <bean id="myBean" class="com.example.MyBean" autowire="byType"/> ``` - **引入(inner beans)**:在一个 Bean 定义中嵌套另一个 Bean 定义。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="innerBean"> <bean class="com.example.InnerBean"/> </property> </bean> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值