码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-04-04 21:04:32      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:有一个   tostring   之间   package   jdbc   自动   tca   setname   gpe   

 

第七周

所花时间

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

 

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

标签:有一个   tostring   之间   package   jdbc   自动   tca   setname   gpe   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!