Spring (Read external database configuration information, annotation-based management bean, DI)

Source: Internet
Author: User
Tags connection pooling



# # # parsing external configuration file
Under the Resources folder, create a new db.properties (and database connection related information)

Driverclassname=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/db
Username=root
Password=root

Development steps
1) Create a MAVEN project
Add Web. xml
Add Tomcat Runtime Environment
Add Jar Spring-webmvc,junit,commons-dbcp,mysql
Add Application.xml
2) connection pooling information for the configuration database

 <!--1.util:properties represents reading an external property file and instantiating the object 2.id representing the name 3.location representing the location of the property file <util:properties id= "dbconf" location= " Classpath:db.properties > < connection pooling for!--configuration database 1. Use spring expressions to assign a value to an attribute 2.spring expression syntax format: #{}- -<BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"><BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">     < Propertyname= "Driverclassname"value= "#{dbconf.driverclassname}"/>     < Propertyname= "url"value= "#{dbconf.url}"/>     < Propertyname= "username"value= "#{dbconf.username}"/>     < Propertyname= "Password"value= "#{dbconf.password}"/> </bean>


# #基于注解的bean管理
# 1. Instantiating an object based on annotations (recommended)
    instantiating objects and dependency injection in two ways: Configuration file, annotations

1. Scan package   (profile)
<!- -Scan package can scan all class--> under current package and sub-package;
     <context:component-scan  base-package= "Cn.tedu.dao" >

2. Annotations for specific functions ( instantiation function)
      @Component general annotations: instantiating objects
      @ Controller: Instantiating objects of the control layer class
      @Service: Instantiating objects of the business layer class
      @Reponsitory: Instantiating objects of the persistence layer class

@Repository ("Userdao")      publicclassimplements  userdao{             publicvoid  Insertuser () {                 System.out.println ("added successfully! ");        }

In MyBatis, it is generally defined an interface in which an abstract method is defined and the corresponding abstract method is implemented in the configuration file.

#2. Life Cycle Management

//@Component represents an instantiated object@Component Public classBeanlife { PublicBeanlife () {System.out.println ("Beanlife");}//@PostConstruct represents a method of defining initialization//@PostConstruct: Tomcat run environment-dependent jar package@PostConstruct Public voidinit () {System.out.println ("Init");} Public voidExecute () {System.out.println ("Execute");}//@PreDestroy represents a method for defining destruction//@PreDestroy: Tomcat run environment-dependent jar package@PreDestroy Public voiddestroy () {System.out.println ("Destroy");}}



# #3. Bean scope

// instantiate an object with annotations, default to Singleton Singleton // @Scope Define the scope of the bean // @Scope ("prototype") indicates that the bean scope is a multi-instance @Component @scope ("prototype")  Public class Demoscope {}


# #4. Lazy Loading

Instantiation of the default Bean object, which is immediately loaded
@Lazy to set whether the bean deferred loading annotations
@Lazy (True) to set the Bean object for lazy loading

@Component @lazy (true)  Public class Demolazy { public demolazy () {System.out.println ("Demolazy");}}



#DI (dynamically provide the object it wants to an object)

Reference: http://www.360doc.com/content/18/0125/09/27831725_724899826.shtml
##[email protected] (recommended)

[email protected] The Tomcat runtime environment relies on annotations defined in the JAR package
[Email protected] implementing Dependency Injection
[email protected] to implement dependency injection, you can omit the Set method
[email protected] The default dependency injection method is ByName
[Email protected] If there are no matching attributes
Implement dependency injection in bytype manner
[Email protected] (name= "Userdaoimpl")

  @Resource (name= "Userdaoimpl")     private Userdao Userdao;




##[email protected] (Learn)

public class USERSERVICEIMPL2 implements userservice2{
[Email protected] Dependency Injection
[Email protected] Default Dependency injection method Bytype;
If there are multiple objects of the same type, then follow the byname dependency injection
3. If you use ByName to implement dependency injection,
Use @qualifier annotations to define matching names
[email protected] cannot be used alone

@Autowired @qualifier ("Userdaoimpl") Private Userdao Userdao;  Public void AddUser () {Userdao.insertuser ();}}


##[email protected] (Learn)

@Component  Public class Student {//@Value ("Admin") to string or base data type Dependency Injection //@Value ("#{conf.name}") Use the spring expression to implement a dependency injection @Value ("#{conf.name}")private  String name;  Public String toString () {return ' name= ' +name;}}

Spring (Read external database configuration information, annotation-based management bean, DI)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.