1. All the classes can be given to spring management
2, how to give a class to bean management?
(1) Configuration Applicationcontext.xml
(2) Write the bean node configuration to the bean-managed class in XML
3. Program Test
(1) To import the Spring core container jar package, the spring kernel contains the following packages:
(2) Create a new Applicationcontext.xml file, this file will be placed under the SRC folder, otherwise cannot find the file, configure the root node beans, and specify the namespace:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <Bean></Bean></Beans>
(3) Configure the Bean node, assuming that there is a user entity class
Public classUser {intID; String name; String password; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } }
(4) configuration to bean-managed format You can use the ID to specify an object name,
You can also use name to specify multiple aliases.
At the same time, class specifies the fully qualified name of the class that the bean is associated with,
If you want to initialize, you can specify Init-method,
If you need to work when destroying, specify Destory-method
The default is singleton mode, if not singleton mode, you need to specify Scope= "prototype" in scope
If you want to specify lazy loading, specify Lazy-init
<id= "user1" class= "Com.spring1.entity.User" Init-method = "Init" destroy-method= "destory"/>
Test in JUnit:
@Test Public void Test () { new classpathxmlapplicationcontext ("Applicationcontext.xml"); = (User) ac.getbean ("user1"); }
You can see that the Get object succeeds.
Spring's bean Management