Spring基于注解的配置之@Autowired

学习地址:https://www.w3cschool.cn/wkspring/rw2h1mmj.html

@Autowired注释

@Autowired注释对在哪里和如何完成自动连接提供了更多的细微控制。

Setter方法中的@Autowired

SpellChecker.java:

package com.lee.autowired;

public class SpellChecker {
    public SpellChecker() {
        System.out.println("Inside SpellChecker constructor");
    }

    public void checkSpelling() {
        System.out.println("Inside checkSpelling");
    }

}

TextEditor.java:

package com.lee.autowired;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
    private SpellChecker spellChecker;

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    @Autowired
    public void setSpellChecker(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }

}

MainApp.java:

package com.lee.autowired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans2.xml");
        TextEditor td = (TextEditor) context.getBean("textEditor");
        td.spellCheck();

    }

}

Beans2.xml:

<?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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="textEditor" class="com.lee.autowired.TextEditor"></bean>
    <bean id="spell" class="com.lee.autowired.SpellChecker"></bean>

</beans>

属性中的@Autowired属性

只有TextEditor不一样:

package com.lee.autowired2;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
    @Autowired
    private SpellChecker spellChecker;

    public TextEditor() {
        System.out.println("Inside TextEditor constructor");
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

构造函数中的@Autowired属性

package com.lee.autowired2;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {

    private SpellChecker spellChecker;
    @Autowired
    public TextEditor(SpellChecker spellChecker) {
        this.spellChecker=spellChecker;
        System.out.println("Inside TextEditor constructor");
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

原文地址:https://www.cnblogs.com/lemon-le/p/9317091.html

时间: 2024-08-30 02:35:25

Spring基于注解的配置之@Autowired的相关文章

Spring框架bean的配置(3):基于注解的配置,Autowired 自动装配 Bean,泛型依赖注入

1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Controller: 标识表现层组件 建立接口:UserRepository package com.atguigu.spring.beans.annotation.test; public interface UserRepository { void save(); } 建立类:UserReposito

(spring-第4回)spring基于注解的配置

基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样一样的. 一.举个栗子: 1 package com.mesopotamia.annotation; 2 3 import org.springframework.stereotype.Component; 4 5 @Component 6 public class Car { 7 private

spring基于注解和配置源码解读

我们先来建立一个maven项目,引入spring文件,不爱弄的在文章最下面有代码地址可以去下载.先看,后面自己下载代码自己去尝试.先给你们吧,边尝试边看吧. 一.IOC容器注册组件的方式 1. 基础XML注入Bean 是不是超级简单的,我们由浅入深一点点来. 2. 基于注解的方式来配置 我们通过方法名就可以直接得到我们的对象了,默认就是按照方法来装配.也可以通过@Bean(value="newName") 来指定装配的名字. 3. 按照包扫描的方式装配(重点),使用@Component

java Spring 基于注解的配置(一)

注解引用:1.service.xml 配置注解模式 <?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://w

【Spring】IOC之基于注解的配置bean

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.基于注解的配置 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这3 个注

阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置这里就可以删除了 配置注解 使用@Service注解 开始AOP配置 把通知类交给Spring来管理 在Logger上加注解.之类注意,@Service和@Repository都不合适.因为logger属于三层 所以这里用@Component这个注解来配置 写完上面的@Component的注解后.b

从源码分析 Spring 基于注解的事物

在spring引入基于注解的事物(@Transactional)之前,我们一般都是如下这样进行拦截事物的配置: <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation=&q

spring基于注解的IOC(2)

spring第二天:spring基于注解的IOC以及IoC的案例1.spring中ioc的常用注解 用于创建对象的:Component.Controller.Service.Repository 用于注入数据的:Autowired.Qualifier.Resource.Value 用于改变作用范围的:Scope . 和生命周期相关:PreDestroy .PostConstruct 2.案例使用xml方式和注解方式实现单表的CRUD操作 持久层技术选择:dbutils3.改造基于注解的ioc案例

Spring基于java的配置

第一步:在XML中配置java信息,与自动检测配置XML一样: <context:component-scan base-package="com.springinaction.springidol"> </context:component-scan> 第二步:定义配置类 第三步:声明bean和bean的注入: package com.springinaction.springidol; import org.springframework.context.a