Spirng-demo

1.导包

核心容器

spring-context
spring-beans
spring-core
spring-expression
commons-logging

2.写配置

pojo

package com.zh.pojo;

public class Animal {
private String name;
private String age;

    public Animal() {
        System.out.println("animal被创建");
    }

    public Animal(String name, String age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("调用set方法"+name);
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

spring-config.xml

<bean id="animal" class="com.zh.pojo.Animal">
<property name="name" value="zh"/>
<property name="age" value=12/>
</bean>

IocText

public void getBean(){
ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-config.xml");
Animal a = (Animal)ioc.getBean("animal1");
System.out.println(a);
}

总结:

   1).ApplicationContext(IOC容器的接口)
   2).给容器中注册一个组件,根据id拿到注册的组件
   容器中对象的创建在容器创建完成的时候就已经创建好了
   3).同一个组件在ioc容器中是单实例的,容器启动完成都已经创建好了
   4).ioc容器在创建组件对象时,(property)会利用setter方法为JavaBean的属性进行赋值
   5).javaBean的属性名是由setter/getter方法决定的
1)ApplicationContext(IOC容器的接口)
2)容器创建完成时,所有组件已经创建好了
3)容器中的组件是单实例的吗
4)ioc容器在创建组件时,(property)利用setter方法为javaBean赋值
5)javaBean的属性名是由Setter/getter方法决定的

spring基础jar包

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.29.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.29.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.29.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.3.29.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13-rc-2</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<!--    c3p连接池-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<!--    基础版面向切面编程-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
<!--  加强版面向切面编程-->
    <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
    <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
<!--    <dependency>-->
<!--        <groupId>net.sourceforge.cglib</groupId>-->
<!--        <artifactId>com.springsource.net.sf.cglib</artifactId>-->
<!--        <version>2.1.3</version>-->
<!--    </dependency>-->

<!--    &lt;!&ndash; https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance &ndash;&gt;-->
<!--    <dependency>-->
<!--        <groupId>org.aopalliance</groupId>-->
<!--        <artifactId>aopalliance</artifactId>-->
<!--        <version>1.0.0</version>-->
<!--    </dependency>-->
    <!-- https://mvnrepository.com/artifact/org.aspectj/com.springsource.org.aspectj.weaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
posted @ 2021-04-26 11:26  Zh'Blog  阅读(49)  评论(0编辑  收藏  举报