Build a spring environment and experience the IOC container ~

Source: Internet
Author: User

Since the most recent task is about converting the format of the IOC configuration file, you need to start learning from the Spring IoC container. Today, based on the introduction on the Internet, we have set up the spring environment, the IOC container has a preliminary experience. The software involved in the article and the recommended Spring IoC container ebook will be appended with my Baidu Network Disk at the end to prevent maintenance on the official website or other floating cloud situations. Appendix ~

1. Open http://www.springsource.org/spring-community-download. as shown in, click take me to the download page.

2. Go to the download page and select the latest version for download.

3. After the download is complete, decompress the package to any folder. The directory structure is clear at a glance.

4. Open eclipse, create a Java project, right-click the project name → build path → configure build path, and select Add external jars in the open window.

5. Then in the jar selection window find the path you just decompressed, enter the libs folder, select spring-beans-3.2.1.RELEASE.jar, spring-context-3.2.1.RELEASE.jar, spring-core-3.2.1.RELEASE.jar, spring-expression-3.2.1.RELEASE.jar, open and click OK to add successfully.

6. Open Logging

7. After the preparation is complete, start the initial experience of IOC and create the following directory structure:

8. The code for each file is as follows:

Beana. Java

1 package org.beans;2 3 public class BeanA {4     public void say(){5         System.out.println("welcome");6     }7 }

Beanb. Java

 1 package org.beans; 2  3 public class BeanB { 4     private BeanA ba ; 5  6     public BeanA getBa() { 7         return ba; 8     } 9 10     public void setBa(BeanA ba) {11         this.ba = ba;12     }13     14 }

Start. Java

 1 package org.beans; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 public class Start { 7     public static void main(String[] args) { 8         ApplicationContext ctx = new ClassPathXmlApplicationContext("org/beans/applicationContext.xml"); 9         BeanB bb = (BeanB) ctx.getBean("beanB");10         bb.getBa().say();11     }12 }

Applicationcontext. xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans  3     xmlns="http://www.springframework.org/schema/beans" 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5     xmlns:p="http://www.springframework.org/schema/p" 6     xsi:schemaLocation="http://www.springframework.org/schema/beans  7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 8  <bean id = "beanB" class = "org.beans.BeanB"> 9      <property name = "ba" ref = "beanA"/>10  </bean>11  <bean id = "beanA" class = "org.beans.BeanA"/>12 </beans>

After running the program, the console outputs welcome ~~

The jar package involved in this article and the introduction to the Spring IoC container. Download link:

Http://pan.baidu.com/share/link? Consumer id = 408901 & UK = 152821134

If you have any shortcomings, I hope the experts will point out that if you have any questions, you are welcome to discuss them ~~

 

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.