Spring Basic, springbasic

Source: Internet
Author: User

Spring Basic, springbasic

@ Spring IOC & DI

1. IOC (Inverse of Control): the idea is to reverse the direction of resource acquisition.

2. DI (Dependency Inject): another expression of IOC

@ Spring configure Bean

1. property injection <property name = "" value = "">

2. constructor-arg value = "" index (assigned by order) = "" type (assigned by type) = "">

3. Factory mode Injection

Details:

1). If the value contains special characters <! [CDATA []> package.

2) The attribute value can be configured with the value node.

3). assign a null value to the private tag <constructor-arg> <null/> </constructor-arg>

4). assign a value to the cascade attribute. The attribute must be initialized before the cascade attribute can be created. Otherwise, an exception occurs, which is different from struts2.

5) if there is a constructor with parameters, there must be a constructor without parameters, otherwise the bean configuration will fail.

<Constructor-arg ref = "car"> <property name = "car. name" value = "">

4. Reference relationship between beans <property name = "'" ref (referenced object) = ""> or subnode <ref bean = "">

5. Internal bean

<Bean id = "" class = ""> <property name = ""> <bean class = ""> <constructor-arg value = ""/> </bean> </property> </bean>Iner bean

6. Set attributes

1). set, List, Map, props, p

Public class DataSource {private Properties properties; public Properties getProperties () {return properties;} public void setProperties (Properties properties Properties) {this. properties = properties ;}@ Override public String toString () {return "DataSource [properties =" + properties + "]" ;}} public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("beans. xml "); DataSource dataSource = ctx. getBean (DataSource. class); System. out. println (dataSource);} <bean id = "dataSource" class = "com. nyan. bean. dataSource "> <property name =" properties "> <props> <prop key =" driverClass "> jdbc: mysql: /// test </prop> <prop key = "jdbcUrl"> com. mysql. jdbc. driver </prop> <prop key = "user"> root </prop> <prop key = "password"> passw0rd </prop> </props> </property> </bean>Props config

2). (SET) configure the bean of the singleton for reference by multiple beans. You need to import the util namespace. Internal beans cannot be referenced.

<Util: list id = "cars">

<Ref bean = "car1">

<Ref bean = "car2">

</Util: list>

<Bean id = "" class = "Person"

<Property name = "cars" ref = "cars">

</Bean>

@ Automatic assembly

1. Specify the automatic assembly mode in the autowire attribute of <bean>

2. byType is automatically assembled Based on the bean type and the type of the current bean attribute. It is not recommended if the type (object has multiple beans) is not unique.

3. byName is automatically assembled Based on the bean name and the setter-style attribute name of the current bean (the bean id is consistent with the object property), constructor

4. If a match exists, it is automatically assembled. If no match exists, it is not assembled.

@ Relationship between beans (inheritance on configuration and dependency)

1. Inheritance: The child bean can ignore the class and be obtained through the parent bean.

2. Dependency: depends-on sets the Bean pre-dependent. The pre-dependent Bean will be created before Bean instantiation. If the front-end is dependent on multiple beans. You can configure the Bean name using commas and spaces.

<! -- Abstract bean: bean whose abstract attribute is true cannot be instantiated by the ico container and can only be used for inherited configuration --> <bean id = "address" class = "com. nyan. bean. autowire. address "p: city =" ShenZhen "p: stree =" NamLu3002 "abstract =" true "> </bean> <bean id =" address1 "class =" com. nyan. bean. autowire. address "p: city =" ShangHei "parent =" address "> </bean> <bean id =" car "class =" com. nyan. bean. autowire. car "p: brand =" OUDI "p: price =" 32000.0 "> </bean> <bean id =" person "class =" com. nyan. bean. autowire. person "p: name =" Nyan "p: address-ref =" address1 "depends-on =" car "> </bean>Abstract & depends

@ Bean Scope

1. singleton: when the container is initialized (when new ClassPathXmlApplicationContext (), the bean instance is created. Only this bean is created throughout the lifecycle of the container, which is a singleton.

2. prototype: prototype. when the container is initialized, no bean instance is created, and a new bean instance is created and returned every time the getBean () request is sent.

@ Use an external property file (system deployment details, such as connecting to the data source) $ {key name}

<! -- Import the property file --> <context: property-placeholder location = "classpath: db. properties "/> <bean id =" dataSource "class =" com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" user "value =" $ {user }) "> </property> <property name =" password "value =" $ {password} "> </property> <property name =" driverClass "value =" $ {driverClass} "> </property> <property name =" jdbcUrl "value =" $ {jdbcUrl} "> </property>Import outer file

@ SpEL (spring Expression Language) Dynamic assignment

1. Reference beans, properties, and methods (reference other objects and properties and call method Chain Operations)

2. Supported operator numbers

<Bean id = "address" class = "com. nyan. bean. spel. Address"> <! -- Use spel to assign a literal value to the attribute --> <property name = "city" value = "# {'beijing'}"> </property> <property name = "stree" value = "diwjijojpo"> </property> </bean> <bean id = "car" class = "com. nyan. bean. spel. car "> <property name =" brand "value =" OUDI "> </property> <property name =" price "value =" 43000.0 "> </property> <! -- Use SpEL to reference static attributes of the class --> <property name = "typePermiter" value = "# {T (java. lang. math ). PI * 80} "> </property> </bean> <bean id =" person "class =" com. nyan. bean. spel. person "> <property name =" car "value =" # {car} "> </property> <property name =" city "value =" # {address. city} "> </property> <! -- Use the operator --> <property name = "info" value = "# {car. price> 30000? 'Golden limit': 'white-collar '} "> </property> </bean>SpEL

 

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.