zoukankan      html  css  js  c++  java
  • 02-Spring的IOC示例程序(通过id获取对象)

    *******通过IOC容器创建id对象并为属性赋值********

    整体结构:

    ①创建一个java工程

    ②导包

    ③创建log4j.properties日记配置文件

    # Global logging configuration
    log4j.rootLogger=INFO, stdout
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    ④创建spring配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      ***********此处是手写代码************
    </beans>

    ⑤创建javaBean对象

    package cn.aynu.pojo;
    
    public class Person {
        private int id;
        private String name;
        private String phone;
        private int age;
        public Person() {
            super();
        }
        public Person(int id, String name, String phone, int age) {
            super();
            this.id = id;
            this.name = name;
            this.phone = phone;
            this.age = age;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person [id=" + id + ", name=" + name + ", phone=" + phone + ", age=" + age + "]";
        }
        
    }

    ⑥在application.xml中配置bean对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--id值(p1)不可重复,如果重复会报错,编译不通过 -->
    <bean id="p1" class="cn.aynu.pojo.Person">
        <property name="id" value="1001"/>
        <property name="name" value="琥珀"/>
        <property name="phone" value="18337280000"/>
        <property name="age" value="18"/>
    </bean>
    
    </beans>

    ⑦写测试类(从容器中获取bean对象)

    public class PersonTest {
        @Test
        public void Test01() {
            //获取spring容器对象(classpath:可略)
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application.xml");
            //根据id获取bean对象
            Person person = (Person) applicationContext.getBean("p1");
            System.out.println(person);
        }
    }

     补充:①如果有多个对象同时获取P1,只有一次输出(单例模式)

        ②优先调用无参构造器中的内容

      

  • 相关阅读:
    403
    311
    401
    310
    308
    309
    307
    304
    3-1
    2-11
  • 原文地址:https://www.cnblogs.com/zly123/p/11637470.html
Copyright © 2011-2022 走看看