Spring4.0.6 latest stable version new features learning, simple learning tutorial (1), spring4.0.6 stable version

Source: Internet
Author: User

Spring4.0.6 latest stable version new features learning, simple learning tutorial (1), spring4.0.6 stable version

Spring Framework 4.0.

The core part of the Spring framework is the Ioc container, while Ioc controls various beans. The level of a Spring Project can be omitted from its XML configuration file. Many projects, it is often a project of an outsourcing company. configuration files are often messy and can be written with the ability to run, without making any mistakes. Then, in the middle and late stages of the project, we can find all kinds of missing items and make up for them, the result is that the entire document is very readable and has no rules. This cannot be blamed for the XML writing. It is the result of forcing programmers to do the architect's work with their wages. For the happiness of programmers, I think it is necessary to summarize and explain the core configurations in a simple and fast official document, so that they can improve themselves and the overall level of projects correctly and quickly in their work.

Check the Code:

Package com. herman. ss. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. herman. ss. pojo. person;/*** @ see new stable version of spring4.0.0. In simple cases, tutorial 1 * @ author Herman. xiong * @ date July 18, 2014 14:49:42 */public class Test0 {/*** @ see spring4.0 simple tutorial 0 */public static void test0 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person0"); // 3. print the bean attribute System. out. println (person0); // 4. copy person0.setName ("herman"); person0.setAge (20); System. out. println (person0);}/*** @ see spring4.0 simple tutorial 1 */public static void test1 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. person can be used to obtain only one bean instance. class obtains beanPerson person0 = (Person) ctx. getBean (Person. class); // 3. print the bean attribute System. out. println (person0); // 4. copy person0.setName ("herman"); person0.setAge (20); System. out. println (person0);}/*** @ see spring4.0 simple tutorial 2 */public static void test2 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain the bean instance. The Person cannot be used. the class obtains the bean, because two Person beans are configured, and beanPerson person0 = (Person) ctx can only be obtained based on the bean id. getBean ("person1"); // 3. print the bean attribute System. out. println (person0);}/*** @ see spring4.0 simple use tutorial 3 use value to assign a value to the object attribute */public static void test3 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person2"); // 3. print the bean attribute System. out. println (person0);}/*** @ see spring4.0 simple use tutorial 4 inject Using bean constructor */public static void test4 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person3"); // 3. print the bean attribute System. out. println (person0);}/*** @ see spring4.0 simple use tutorial 5 Use bean type to assign a value to the attribute */public static void test5 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person4"); // 3. print the bean attribute System. out. println (person0);}/*** @ see spring4.0 simple tutorial 6 Use bean reference object and null object */public static void test6 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person5"); // 3. print the bean attribute System. out. println (person0);}/*** @ see spring4.0 simple use tutorial 7 use bean reference object and use index subscript to assign values to bean properties */public static void test7 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext. xml "); // 2. obtain bean instance Person person0 = (Person) ctx. getBean ("person5"); // 3. print the bean attribute System. out. println (person0);} public static void main (String [] args) {// test0 (); // test1 (); // test2 (); // test3 (); // test4 (); // test5 (); // test6 (); test7 ();}}

<Bean class = "Bean class"

Name/id = "unique name of Bean in container"

Scope = "Bean lifecycle. For more information, see the following"

Autowiring-mode = "this Bean's properties automatic injection method. For more information, see the following"

Lazy-init = "Is it a lazy load? For more information, see"

Init-method = "the callback method after the container initializes the Bean. For more information, see the following"

Destroy-method = "Callback method of the container after the Bean is destroyed. For more information, see"

Abstract = "whether it is an Abstract Bean. It is mainly used to unify the attribute configuration of many beans in the XML configuration document and has no relationship with Java abstract Class"

Parent = "the name of the parent Bean, which inherits the attributes of the parent Bean and is only valid in the configuration document. It has nothing to do with the Java Class"

Factory-method = "Name of the factory method"

Factory-bean = "Name of factory Bean"

Depends-on = "depends on the Bean name. Spring ensures that the Beans to be depended on will be initialized before the Bean is initialized. This attribute will not be inherited by Bean, the child Bean needs to re-write its own depends-on"

Autowire-candidate = "whether it is a candidate for automatic injection. Generally, when the autowiring-mode attribute of other beans is set to automatic search, this Bean can be avoided or allowed to be included in the matching list"

Primary = "whether to set this Bean as the first choice among candidates for automatic injection of other beans"

>

// Constructor-arg: attribute Assignment Method 1

<Constructor-arg type = "int" value = "7500000"/>

// Constructor-arg method 2

<Constructor-arg name = "years" value = "7500000"/>

// Constructor-arg method 3

<Constructor-arg index = "0" value = "7500000"/>

// Attribute assignment in Properties Mode

<Property name = "beanOne">

<Ref bean = "name of another Bean"/>

</Property>

// Attribute assignment in Properties Mode

<Property name = "beanOne" ref = "name of another Bean"/>

// Attribute Assignment Method 3

<Property name = "integerProperty" value = "1"/>

</Bean>

Project Structure:

The project integrates the jar packages of struts2 and spring4.0.6.

Jar package download: http://download.csdn.net/download/xmt1139057136/7649389 click to download

If you have any questions, please join the QQ group: 135430763 to learn together!




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.