Discover spring bean configuration xml file, include the articles, news, trends, analysis and practical advice about spring bean configuration xml file on alibabacloud.com
. Annotationconfigapplicationcontext preparerefresh Info: refreshing org. springframework. Context. Annotation. Annotationconfigapplicationcontext@203e25d3:startup Date [Thu : Each CST 2016] ; root of context hierarchyspring container start initialization ... 2. The @Bean is labeled on the method (the method that returns an instance), equivalent to the spring XML
Start with a simple example:
Scene class Client.java
Package Com.dusk.bean;
Import Org.junit.Test;
Import org.springframework.beans.factory.BeanFactory;
Import org.springframework.beans.factory.xml.XmlBeanFactory;
Import Org.springframework.core.io.ClassPathResource;
Import Org.springframework.core.io.Resource;
public class Client {
@Test public
void Test () {
Resource resource=new classpathresource ("com/dusk/bean/ Bean.xml ");
Beanfactory b
{
User login (user user);
int register (user user);
List
Implement service (COM.HPE.SERVICE.IMPL.USERSERVICEIMPL), note that we are no longer Iuserdao iud=new Userdaoimpl when invoking the DAO interface, this way, Instead, the Iuserdao is handed to the configuration file management, and the bean is used to pass the parameters according to the set method.
Pac
In the Java EE project may involve the use of some framework, recently contacted SSH, take spring to use the configuration file is very important, spring configuration file is an XML
@configuration, but when using XML, you need to declare multiple XML Schema (XSD) files at the top of the configuration file that define the XML elements that configure spring. An easy
One1. When the configuration file is XML, the slice class does not write AOP anotation1 PackageCom.springinaction.springidol;2 3 Public classMagicianImplementsMindReader {4 PrivateString Thoughts;5 6 Public voidInterceptthoughts (String thoughts) {7SYSTEM.OUT.PRINTLN ("Intercepting volunteer ' s Thoughts");8 This. Thoughts =thoughts;9 }Ten One P
The structure of the XML file is generally as followsxmlns is an abbreviation for XML Namespace, which represents the spring namespace. Spring finds all the spring.handlers in Classpath and parses the XML
/sw_database "); //datasource.setuser ("root");//Datasource.setpassword ("root");
String SQL= "INSERT into Sw_user (Username,password) VALUES (?,?)"; intCount=jdbctemplate.update (SQL, "Bullet", "battle-Man song"); if(count==1) {
return true; }
return false; }
} To use the JdbcTemplate object in Userdao, this object is also injected through the config file XML, which needs to be setjdbctemplate
, Spring will automatically instantiate property B if it finds that property B is not instantiated when obtaining property, this is also an important feature provided by Spring. In some cases, B will not be initialized, such as implementing the BeanNameAware interface.
This is introduced in Spring: the specified dependency interfaces are ignored during Automatic
com.atguigu.spring.beans.annotation.test; Import Org.springframework.stereotype.Controller; @Controller // Identify presentation layer components Public class Usercontroller { publicvoid Test () { System.out.println (" panpan789 ");} }The above classes are built under the package com.atguigu.spring.beans.annotation.test;Spring XML configuration
In Spring configuration, the bean class uses the factory Bean meaning for parsing. springbean
Parsing the meaning of factory bean used by Bean class in Spring
The Bean class in Spring configuration uses the meaning of the factory bean to parseThe beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)This article follows "Attribution-non-commercial use-consistent" authoring public agreementReprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog foc
Configuration through the configuration file XML method
can use a very concise service class
The UserService class code is as follows:
Package com.swift;
public class UserService {
private Userdao Userdao;
public void Setuserdao (Userdao userdao) {
This.userdao = Userdao;
}
public boolean Add
We all know that Java reads ordinary files through basic I/O InputStream, OutStream, Reader, Writer and so on. In the spring framework, how does it identify the XML configuration file?This depends on the two interfaces of the IOC container beanfactory and applicationcontext:
beanfactory Interface
|--------
OverviewIt is well known that the Spring framework is a driver of the control inversion (IOC) or Dependency injection (DI) pattern, which is implemented through container-based configuration. In the past, Spring allowed developers to use XML-based configuration to manage
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.