Spring 4.0.2 Study Notes (1)

Source: Internet
Author: User

1. Add maven support

                    org.springframework            spring-context            4.0.2.RELEASE        

Basic injection. You only need this dependency.

 

2. Compile several test classes

Suppose: There is a programmer jimmy who has several computers and a pet. After going home from work every day, the pet will welcome him home in some form.

    
  MacBook  "\tApple MAC Book"
  ThinkPad  "\tLenovo ThinkPad"
    
  Dog  "\twang! wang!"
  List<Computer> .name = List<Computer>  setComputers(List<Computer>.computers = .pet = "My name is " +", and I have " + computers.size() + " computer" + (computers.size() > 1 ? "s" : "") + ":""And I have a pet, everyday,when I go home, it will welcome me by  "

 

3. spring configuration file

                                                                                                            

This configuration file SimpleBeans. in xml, a total of four bean instances are configured. After the configuration is loaded by the Spring container, these objects will be instantiated (by default, the singleton mode is instantiated) and kept in the container, you can use the getBean method of ApplicationContext to obtain

 

4. Test Injection

For ease of injection, first write a simple tool class SpringUtils

    (applicationContext == =   ClassPathXmlApplicationContext("SimpleBeans.xml"   <T> T getBean(Class<T>

Then you can use this tool class to get the injected object instance through getBean.

  =  SpringUtils.getBean(Programmer.,"jimmy"

Running effect:
My name is jimmy. yang, and I have 2 computers:
Lenovo ThinkPad
Apple MAC Book
And I have a pet, everyday, when I go home, it will welcome me
Wang! Wang!

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.