Spring learning and spring framework Learning

Source: Internet
Author: User

Spring learning and spring framework Learning

<! -- Use an external property file to connect to the database --!>
<Context: property-placeholder location = "db. properties"/>
<Bean id = "dataSource" class = "com. mchange. v2.c3p0. ComboPooledDataSource"> <! -- Use an external property file --> <property name = "user" value = "$ {user}"> </property> <property name = "password" value = "$ {password} "> </property> <property name =" driverClass "value =" $ {driverclass} "> </property> <property name =" jdbcUrl "value =" $ {jdbcurl} "> </property> </bean>

Some details are required during system deployment. This information is inconvenient to be stored in the middle and late stages of spring and requires an external attribute file. spring provides a post-processor of PropertyPlaceholderConfigurer, it allows you to move the bean configuration content out of the property file. After writing the configuration file, use "$ {var}" to access the property (note that you must first import the context namespace)

public static void main(String[] args) throws SQLException {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");DataSource dataSource = (DataSource) ctx.getBean("dataSource");                System.out.println(dataSource.getConnection());}

  

user=rootpassword=123456driverclass=com.mysql.jdbc.Driverjdbcurl=jdbc:mysql:///hibernate

SpEL (Spring Expression Language)

# {} Facilitates the Dynamic assignment of bean attributes. You can certainly reference the literal volume with SpEL, but it is useless. You can also reference the attributes of objects and other objects, class static variables, etc.

<Bean id = "car" class = "com. autowire. car "scope =" prototype "> <property name =" car "value = # {audi}> </property> <property name =" tyrePremeter "value =" # {T (java. lang. math ). PI * 80} "> </property>
<Property name = "brand" value = # {brand. name} </property>
<Property name = "price" value = # {300000}> </property>
<Property name = "info" value = "# {price> 400000? 'Gao fushuai ': 'diaosi'} "> </property>
<! -- You can even assign values dynamically --!>
</Bean>

Bean lifecycle in IOC:

The container can manage the object lifecycle. The following example shows the process of IOC object management:

public class Car{ 2    public Car(){ 3         System.out.println("car's constructor");          4    } 5    public void setBrand(String brand){ 6         System.out.println("setBrand") 7   } 8    public void init(){ 9         System.out.println("car's init");10    }    11    public void destory(){12          System.out.println("car's destory");13   }

<! -- Create an object to test --!>
<Bean id = "car" class = "com. process. car "init-method =" init "destroy-method =" destory "> <property name =" brand "value =" Audi "> </property> </bean>
Public static void main (String [] args) {// TODO Auto-generated method stub ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext ("bean-process.xml"); Car car = (Car) ctx. getBean ("car"); System. out. println (car); ctx. close (); // Test Method

 

 

Result:

Car's constructor
SetBrand
Car's init
Com. process. Car @ 3a8624

Car's destory // This is the entire lifecycle process

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.