Spring 中存取 Bean 的相关注解

目录

1.存储Bean对象(五大注解)

1.1 设置扫描根目录

1.2 添加注释存储Bean对象

2.注入并使用Bean

2.1 属性注入

2.2 Setter方法注入

2.3 构造方法注入


1.存储Bean对象(五大注解)

环境配置:先在IDEA创建Maven项目,不用使用任何模板,在pom.xml里引入spring依赖

<?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:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--<bean id="student" class="com.lin.controller.Student"></bean> -->
    <!--配置存储Bean对象的扫描根路径 -->
    <content:component-scan base-package="com.lin"></content:component-scan>
</beans>

1.1 设置扫描根目录

base-package=" 具体包名+类名",可以扫描到根目录里的所有Bean对象

<content:component-scan base-package="com.lin"></content:component-scan>

1.2 添加注释存储Bean对象

将对象存储在Spring,有两种注解类型可以实现

1.类注解:@Controller , @Service , @Repository, @Component , @Configuration

2.方法注解:@Bean

方法注解要搭配类注解进行使用

package com.lin.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:studentBean对象
 * User: yoga460
 * Date: 2022-11-14
 * Time: 17:09
 */

@Component
public class StudentBean {
@Bean
    public Student student(){
        Student student = new Student();
        student.setId(1);
        student.setName("lin");
        student.setScore(70);
        return student;
    }

}

       //1,得到Spring上下文对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring.xml");
            //2.根据名称和类来获取对象
        StudentBean studentBean  = context.getBean("studentBean",StudentBean.class);
        System.out.println(studentBean.student());

 当只有一个构造方法是可以省略@Bean注解,例如上面的代码没有@Bean注解也能运行

2.注入并使用Bean

2.1 属性注入

注入别的类:

@Controller
public class UserController {
    @Autowired
    private StudentBean studentBean;
    public  Student student(){
        return studentBean.student();


    }

@Autowired

要注入的类和对象

类里的方法

注入启动类:

public class App {
@Autowired
private StudentBean studentBean;
    public static void main(String[] args) {


                //1,得到Spring上下文对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring.xml");
            //2.根据名称和类来获取对象
        StudentBean studentBean = context.getBean(StudentBean.class);
        System.out.println(studentBean.student());
}
}

@Autowired

要注入的对象

getBean获取对象

调用对象的构造方法

如果有只有一个构造方法则可以省略@Autowired,有多个的话则需要添加上 @Autowired 来明确指定到底使⽤哪个构造⽅法,否则程序会报错

2.2 Setter方法注入

跟上面差别就是在于@Autowired注解放在类里面的setter方法上

注入类:

@Controller
public class UserController {

    private StudentBean studentBean;

    @Autowired
    public void setStudentBean(StudentBean studentBean){
        this.studentBean = studentBean;
    }

    public UserController(StudentBean studentBean){
        this.studentBean = studentBean;
    }
    public  Student student(){
        return studentBean.student();}

        public void doController(){
            System.out.println("do Controller");
            System.out.println("------------------");
            System.out.println("do StudentBean");
        }


    }

 启动类:

public class App {
//@Autowired
//private StudentBean studentBean;
    public static void main(String[] args) {


                //1,得到Spring上下文对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring.xml");
            //2.根据名称和类来获取对象
       UserController userController = context.getBean("userController",UserController.class);
       userController.doController();
}
}

2.3 构造方法注入

跟上面道理一样,@Autowired放在类的构造方法上

注入类:

@Controller
public class UserController {

    private StudentBean studentBean;

    @Autowired
    public UserController(StudentBean studentBean){
        this.studentBean = studentBean;
    }
    public  Student student(){
        return studentBean.student();


    }
//    public void sayHi(){
//        System.out.println("Hi~");
//    }
}

启动类:

public class App {
@Autowired
private StudentBean studentBean;
    public static void main(String[] args) {


                //1,得到Spring上下文对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring.xml");
            //2.根据名称和类来获取对象
       UserController userController = context.getBean(UserController.class);
        System.out.println(userController.student());
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值