Spring学习进度-02(第七周)


第七周


所花时间


25h左右


代码量


1500行左右


博客量


3篇


学到的知识点


spring中的自动装配,bean之间的关系,bean的作用域,引用外部属性文件

一、自动装配

package zidongpeizhi;

public class Address {
    private String city;
    private String home;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getHome() {
        return home;
    }
    public void setHome(String home) {
        this.home = home;
    }
    @Override
    public String toString() {
        return "Address [city=" + city + ", home=" + home + "]";
    }

}

Address

package zidongpeizhi;

public class Car {
    private String name;
    private double price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}

Car

package zidongpeizhi;

public class Person {
    private String name;
    private Car cars;
    private Address address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Car getCars() {
        return cars;
    }
    public void setCars(Car cars) {
        this.cars = cars;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", cars=" + cars + ", address=" + address + "]";
    }

}

Person

package zidongpeizhi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zidongpeizhi.Person;

public class Main {

    public static void main(String[] args) {
        ApplicationContext c=new ClassPathXmlApplicationContext("zidongpeizhi.xml");
        Person pe=(Person) c.getBean("person");
        System.out.println(pe);

    }

}

Main

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="zidongpeizhi.Address" p:city="ningxia"
        p:home="guang"></bean>
    <bean id="car" class="zidongpeizhi.Car" p:name="dazhong"
        p:price="60"></bean>
        <!--
    <bean id="person" class="zidongpeizhi.Person" p:name="tom"
        p:address-ref="address" p:cars-ref="car"></bean>
         -->
        <!--byname 匹配javabean里的setter
        <bean id="person" class="zidongpeizhi.Person" p:name="tom"
        autowire="byName"></bean>
        -->
        <!-- bytype ioc容器只能有一个匹配 -->
        <bean id="person" class="zidongpeizhi.Person" p:name="mmom"
        autowire="byType"></bean>

</beans>

.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 设置abstract="true"属性,只能被继承 -->
    <bean id="address" p:city="beijing" p:home="z" abstract="true">
    </bean>
    <!-- 继承address -->
    <bean id="address1" class="beanguanxi.Address" p:city="beijing"
        parent="address">
    </bean>
    <!-- 依赖 -->
    <bean id="car" class="beanguanxi.Car" p:name="baoma"
    p:price="100"></bean>
    <bean id="person" class="beanguanxi.Person" p:name="tom"
        p:address-ref="address1" depends-on="car">
    </bean>
</beans>

beanguanxi.xml

三、引用外部属性文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 导入外部文件 -->
    <context:property-placeholder location="classpath:mysql"/>
    <bean id="datasource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverclass}"></property>
        <property name="jdbcUrl" value="${jdbcurl}"></property>
    </bean>
    <!--
    <bean id="datasource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///mmm"></property>
    </bean>
     -->
</beans>

shujuyuan.xml

user=root
password=123456
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///mmm

mysql.txt

package shujuyuan;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args)   {
        ApplicationContext c=new ClassPathXmlApplicationContext("shujuyuan.xml");
        DataSource datasource =(DataSource) c.getBean("datasource");
        try {
            System.out.println(datasource.getConnection());
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

}

Main

原文地址:https://www.cnblogs.com/MoooJL/p/12633857.html

时间: 2024-07-31 23:08:47

Spring学习进度-02(第七周)的相关文章

学习进度条 第七周

这周由于是团队作业而且是大作业形式,代码除了复习数据结构算法写的代码其他基本没有写,安卓的代码只是下下来源码自己在看,自己的任务一旦确定就开始着手完成安卓应用.   第七周 所花时间(H) 11 代码量(行) 220 博客量(篇) 2 了解到的知识点 安卓开发的一些技巧

学习进度_第七周

  第七周 所花时间(包括上课) 12小时 代码量(行) 300行 博客量(篇) 3篇 了解到的知识点 团队开发的流程和团队项目的估计

学习进度条——第七周

  第七周 所花时间(包括上课) 上课:200分钟 看书:75分钟 编程:0小时 代码量(行) 0行 博客量(篇) 2篇 了解到的知识点 1.团队有一致的集体目标,团队要一起完成这目标.一个团队的成员不一定要同时工作 2.在复杂的项目里,要让 一线队员做决定 3.MSF(Microsoft Solution Framework)的9大基本原则 4.要做团队里的鸡或猪

学习进度条第七周

  第六周 所花时间 10小时 代码量 250行左右 博客量 2 了解到的知识点 学习了javaweb连接数据库并对数据库进行简单的操作

学习进度表(第七周)

学习进度表(第七周) 周数 专业学习目标 专业学习时间 新增代码数量 博客发表量 人文方面的学习 知识技能总结 6 队列.循环队列.用代码写出表单 31小时 104 1 <文学回忆录> 栈的输入都是从栈顶开始,先入后出,而队列是先进先出.

学习进度条 第五周

  第五周 第六周 第七周 第八周 学习时间(h) 上课时间:4 编程时间:21 阅读时间:4       代码量(行) 120       博客量(篇) 1       所学到的知识点 1.对MFC有了一些基本的了解. 2.对软件开发中德角色有了更好的了解.      

学习进度条(第一周)

本周上课时间为周一 周四下午的2:00到4:00:课余复习时间为周三下午的3:00到6:00,这段时间主要用来阅读了软件工程中的经典书目<构建之法>:并与周日上午9:00到12:00完成了老师在本周中所布置的任务,像用一个小时写出了关于四则运算的小程序,其余的时间用于完成构建之法阅读笔记的完成和对本周的学习进度的一个简单总结.本周所写代码的行数为28行,此程序主要是用来完成老师所安排的作业.博客量有四则,分别是自我简介,阅读笔记,程序代码和这则博客.收获:通过这周的学习,开始对软件工程有了一个

学习进度条(7-9周)

  第七周  第八周 第九周  所花时间(包括上课) 18个小时     代码量(行) 150     博客量(篇) 3     了解到的知识点 ·这周主要学习的的是工作项估计,对于我们现在,甚至是以后找到工作都是很重要的.

学习进度条(七)

  第七周 所花时间(包括上课) 上课2小时,看书1.5小时,编程约4小时 代码行量 120行左右 博客量篇  3篇 了解到的知识点 1 需求调研和团队合作 2.软件项目估计