Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of annotations of Spring-IOC Container and Bean Management in Java

Shulou Source: shulou.com Published: 2022-06-03 20:06:53 11月12日 Update

This article will explain in detail about Spring-IOC container and Bean management annotations in Java sample analysis, Xiaobian think quite practical, so share to everyone as a reference, I hope you can read this article after harvest.

Spring-IOC Container-Bean Management-Annotations Based Approach What are annotations?

(1) Annotations are code special tags, format: @ annotation name (attribute name = attribute value, attribute name = attribute value…)

(2) Use annotations, annotations on classes, methods, attributes

(3) Purpose of using annotations: simplify xml configuration

Spring provides annotations for objects created in Bean Management

The following four annotations function the same and can be used to create bean instances

(1)@Component

(2)@Service

(3)@Controller

(4)@Repository

Object Creation Based on Annotations

① Introducing dependencies (introducing spring-aop jar package)

② Start component scanning

③ Create a class and add an object creation comment to the class.

//In the annotation, the value attribute value can be omitted.//The default value is the class name, the first letter is lowercase.//UserService -- userService@Component(value = "userService") //The annotation is equivalent to the XML configuration file: public class UserService { public void add() { System.out.println("service add....... "); }} Enable Component Scan Details Configuration Attribute Injection Based on Annotations

① @Autowired: Automatic assembly according to attribute type

(1) The first step is to create the service and dao objects, and add the creation object annotation to the service and dao classes.

(2) The second step is to inject dao object into service, add dao type attribute to service class, and use annotation on attribute.

@Servicepublic class UserService { //Define dao type properties//Do not need to add set methods//Add injected properties annotation @Autowired private UserDao userDao; public void add() { System.out.println("service add...... "); userDao.add(); }}/Dao implements @Repository//@Repository(value = "userDaoImpl1")public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("dao add..... "); }}

② @Qualifier: inject according to the name, use this @Qualifier annotation, and use it with @Autowired above

//Define dao type attributes//No need to add set method//Add injection attribute annotation @Autowired //Injection according to type//Injection according to name (The purpose is to distinguish multiple implementation classes under the same interface, and it is impossible to select according to type, so an error occurs!)@ Qualifier(value = "userDaoImpl1") private UserDao userDao;

③ @Resource: It can be injected according to type or name (it belongs to the annotation under javax package, not recommended!)

//@Resource //inject by type @Resource(name = "userDaoImpl1") //inject by name private UserDao userDao;

④ @Value: inject common type attribute

@Value(value = "abc")private String name Full annotation development

(1) Create configuration classes instead of xml configuration files

@Configuration //As a configuration class, replace xml configuration file @ComponentScan(basePackages = {"com.atguigu"})public class SpringConfig { }

(2) Writing test classes

@Testpublic void testService2 () { //Load Configuration ClassApplicationContext context = new AnnotationConfigApplicationContext (SpringConfig.class); UserService userService = context.getBean ("userService",UserService.class); System.out.println (userService); userService.add();} About "Spring-IOC container in Java and Bean management annotation sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Tags: Annotations properties types configurations names objects management containers files methods methods articles examples analysis more purposes components good practical ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Microsoft MariaDB Shulou Information