In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.