In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of the basics of bean in Spring", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of the basics of bean in Spring".
Bean:
In Spring technology, it is based on components.
The most basic is the most commonly used unit.
Actually, the instance is stored in the container of Spring.
Bean is usually defined in the configuration file. Bean instantiation is managed by Spring's Ioc container, and instances of Bean can be accessed through Beanfactory. In fact, for most J2EE applications, Bean is accessed through ApplicationContext, and ApplicationContext is a subinterface of BeanFactory, which is much more powerful than BeanFactory.
In the previous blog dependency injection and control inversion demonstrated the use of spring to achieve ioc, there is a configuration of bean in ApplicationContext.xml, which is only a simple application of bean. The main purpose of this article is to further learn to use bean.
I. definition
The above code is configured by the previous blog, and you can see the basic composition of bean. Is the root node of the Sring configuration file, and there can be multiple nodes in a node. There are two attributes commonly used in bean: ID,Class. ID is a unique identifier to determine which bean it is, which allows id references to be used in other bean. Class is used to specify which class. You can also set the scope property, and there are two types of scope: singleton,non-singelton. Singleton: single instance mode (default, constructor is private), only one shared instance exists in the container of the entire Spring (singleton). Non-singelton: each time the bean,Spring container is requested, a new bean instance is created and returned to the program (request,session,prototype).
Second, create
Naming Mechanism of Bean
Id when looking for a Bean object in the window of Spring, it first looks according to id, using the rest as the default name of Bean, and if the ID attribute does not exist, it looks according to the Name attribute (using the first name as the default name), if neither ID nor NAME exists to look up based on the name of the class. Id- > name- > class name.
Alias for Bean: you can use alias to specify an alias for bean.
The following configuration file is modified based on the above xml. ID is configured here to set an alias for ServiceImpl's bean. We can use its name, id, alias to get service.
Package Cuiyw.SpringAop;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import Cuiyw.Spring.IService.IService;public class App {public static void main (String [] args) {ApplicationContext context=new ClassPathXmlApplicationContext (new String [] {"ApplicationContext.xml"}); BeanFactory factory=context; IService service= (IService) factory.getBean ("ServiceA1"); service.service ("Cuiyw ServiceA1"); service= (IService) factory.getBean ("ServiceA"); service.service ("Cuiyw ServiceA") Service= (IService) factory.getBean ("ServiceImpl"); service.service ("Cuiyw ServiceImpl");}
III. Injection
1. Basic types and string
You can use the value element to set it and modify it slightly based on the above code
Package Cuiyw.Spring.Service;import Cuiyw.Spring.IDao.IDao;import Cuiyw.Spring.IService.IService;public class ServiceImpl implements IService {private IDao dao; private int baseProperty; public IDao getDao () {return dao;} public void setDao (IDao dao) {this.dao = dao;} public void service (String name) {System.out.println (dao.sayHello (name) + "baseProperty:" + getBaseProperty ());} public int getBaseProperty () {return baseProperty } public void setBaseProperty (int baseProperty) {this.baseProperty = baseProperty;}}
For the string type, the XML parser parses the data with the String type. If the property is not a String type, the property value is converted to another type through PropertyEditors, such as the time type.
two。 Inject bean
Bean is injected into the above code, and DaoImpl is injected into ServiceImpl. You can use ref for configuration.
3. Injection set
Common collections are list, map, set, property, and so on. The following code defines several properties in ServiceImpl and then injects them into the context through attributes. For testing, an attribute s has also been added to DaoImpl.
Package Cuiyw.Spring.Dao;import java.util.Calendar;import Cuiyw.Spring.IDao.IDao;public class DaoImpl implements IDao {public String s; public String getS () {return s;} public void setS (String s) {this.s = s;} public String sayHello (String name) {int hour=Calendar.getInstance () .get (Calendar.HOUR_OF_DAY); if (hour)
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.