Spring 中存取 Bean 的相关注解

bean的储存

要把某个对象交给Ioc容器管理,需要在类上加上注解如@component,而spring框架提供了很多的注解

主要分为两类:

类注解:@Controller、@Service、@Repository、@Component、@Configuration

方法注解:@Bean

@controller(控制储存器)

使用@controller存储bean

import com.example.demo.Controller.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller//将对象存储到spring中
public class UserController {
    @Autowired
private UserService userService;
    public void doController()
    {
        userService.doservice();
        System.out.println("doController");
    }

}

获取到bean

import com.example.demo.Controller.UserController;
import com.example.demo.Controller.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
			//spring上下文
		ApplicationContext context=(ApplicationContext)SpringApplication.run(DemoApplication.class, args);//spring获取上下文
		UserController bean=context.getBean(UserController.class);//从spring中获取对象
		bean.doController();//对象调用方法
	}

}

运行结果:

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

 如果把@controller删除会报错

找不到UserController的bean

@service(服务存储)

使用@service储存bean


import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;


@Service
public class UserService {
    public void doservice()
    {
        System.out.println("doservice");
    }
}

获取到bean

import com.example.demo.Controller.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
			//spring上下文
		ApplicationContext context=(ApplicationContext)SpringApplication.run(DemoApplication.class, args);
		UserService bean1=context.getBean(UserService.class);
		bean1.doservice();
	}
}

运行结果

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

@Repository(仓库存储)

使⽤@Repository存储bean

import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
    public void doPosistroy()
    {
        System.out.println("doPository");
    }
}

获取bean

import com.example.demo.dao.UserRepository;
import com.example.demo.servise.UserSerVies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) 
	{
		ApplicationContext context=(ApplicationContext)                         
        SpringApplication.run(DemoApplication.class, args);
		UserRepository bean3=context.getBean(UserRepository.class);
		bean3.doPosistroy();
	}


}

运行结果:

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

@Component(组件存储)

使用@Component存储Bean

import org.springframework.stereotype.Component;

@Component
public class UserCompnent {
    public void doComponent()
    {
        System.out.println("doComponent");
    }
}

读取Bean

import com.example.demo.component.UserCompnent;
import com.example.demo.configuration.UserConfirgration;
import com.example.demo.controller.UserController;
import com.example.demo.dao.UserRepository;
import com.example.demo.servise.UserSerVies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) 
	{
		ApplicationContext context=(ApplicationContext) 
        SpringApplication.run(DemoApplication.class, args);
		UserCompnent bean4=context.getBean(UserCompnent.class);
		bean4.doComponent();

	}


}

运行结果:

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

@Configuration(配置存储)

使用@Configuration存储Bean

import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfirgration {
    public void UserComponpent()
    {
        System.out.println("doconfiguration");
    }
}

读取Bean

{

	public static void main(String[] args) 
	{
		ApplicationContext context=(ApplicationContext) 
        SpringApplication.run(DemoApplication.class, args);
		UserConfirgration bean5=context.getBean(UserConfirgration.class);
		bean5.UserComponpent();
	}


}

运行结果:

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

 

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