[] Spring "Hello World"

Source: Internet
Author: User

0, written in the preceding words this article, in a simple example, describes spring's loading and fetching of Java classes through containers.in the following we can see that there is a Java class coder, we did not manually call new to instantiate, but from the spring container applicationcontext to get the instantiation of the class object。
1. Build a spring project with Maven
  
 
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>4.3.16.RELEASE</version>
  6. </dependency>
  7. </dependencies>

Using MAVEN to build a project, Dependency,ide automatically downloads associated spring-related core packages, such as: 2, to create a simple Java class by spring's official offer.
  
 
  1. public class Coder {
  2. private String name;
  3. private int age;
  4. public void print(){
  5. System.out.println("Hello World");
  6. }
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. }

3. Create a spring configuration file for idea, Pom.xml has already configured spring dependencies, so when creating a new spring configuration file, you can create the most basic configuration file directly from idea: Then configure the Java class Bean:
  
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean ID="Coder" class="Dulk.learn.spring.Coder"></bean>
  6. </beans>

4, initialize the container, get the instance spring provides a variety of ways to initialize containers, the most common of which are two:
    • Beanfactory, early JavaBean factory class implementation ( not recommended )
    • ApplicationContext, the extension of beanfactory, provides more functions, such as internationalization processing, bean automatic assembly, etc.

beanfactory
  
 
  1. public class Test {
  2. public static void main(String[] args) {
  3. Beanfactory
  4. ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
  5. Resource resource = rpt.getResource("/applicationContext.xml");
  6. BeanFactory bf = new XmlBeanFactory(resource);
  7. Coder coder = (Coder) bf.getBean("coder");
  8. coder.print();
  9. }
  10. }

ApplicationContext
  
 
  1. public class Test {
  2. public static void main(String[] args) {
  3. ApplicationContext
  4. ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
  5. Coder coder = (Coder) ac.getBean("coder");
  6. coder.print();
  7. }
  8. }

[] Spring "Hello World"

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.