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依赖



    
    
    

1.1 设置扫描根目录

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

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;
    }

}

Spring 中存取 Bean 的相关注解_第1张图片

       //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获取对象

调用对象的构造方法

Spring 中存取 Bean 的相关注解_第2张图片

如果有只有一个构造方法则可以省略@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());
}
}

你可能感兴趣的:(JavaEE,spring,java)