spring入门:关键点整理(1)-- 创建对象及注入属性

一:导入jar包及创建配置文件

目录结构:

二:创建对象的3种方式

1.构造方法创建bean

2.静态工厂创建bean

3.工厂方法创建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" 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.xsd">

    <!--创建对象 方法一:默认为构造方法创建对象-->
    <bean id = "student" class = "com.csdn.pojo.Student"
          init-method="init" destroy-method="destory"
        scope="singleton">
    </bean>


    <!--创建对象 方法二:静态工厂方法创建对象-->
    <bean id = "beanFactory" class = "com.csdn.pojo.BeanFactory" factory-method="getBean"
          scope="singleton">
    </bean>

    <!--创建对象 方法三:工厂方法创建对象(非静态方法)-->
    <bean id = "bf" class="com.csdn.pojo.BeanFactory"></bean>
    <bean id = "students2" factory-bean="bf" factory-method="getBean2">
    </bean>

</beans>

测试类

    @Test
    public void test(){
        //spring的bean容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从bean容器中取出bean
        //从容器中两次取出bean,就否取出的是是同一对象,取决于配置文件中scope的配置。单例与多例。默认为单例
        Object student = ac.getBean("student");
        System.out.println(student);
        Object student2 = ac.getBean("student");
        System.out.println(student2);
        //close方法可以销毁对象
        ((ClassPathXmlApplicationContext) ac).close();
    }


    @Test
    public void test02(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object beanFactory = ac.getBean("beanFactory");
        System.out.println(beanFactory);

        Object students2 = ac.getBean("students2");
        System.out.println(students2);
    }

 

三:注入属性的4种方法

1.set方法注入

2.构造器注入

3.p标签注入

4.el表达式注入

    <bean id = "student" class = "com.csdn.pojo.Student"
          init-method="init" destroy-method="destory"
        scope="singleton">
        <!--属性注入:方法一 属性注入 必须要有set方法-->
        <!--属性注入:方法四 el表达式注入-->
        <property name="age" value="#{student2.age}"/>
        <property name="email" value="#{student2.name}"/>
        <property name="name" value="#{student2.email}"/>
        <!--属性注入:方法二构造器注入 必须要有构造器方法-->
        <!--<constructor-arg name="name" value = "lily"/>-->
        <!--<constructor-arg name="age" value = "30"/>-->
        <!--<constructor-arg name="email" value = "123@qq.com"/>-->

    </bean>
    <!--属性注入:方法三 p标签注入,实际上也为属性注入-->
    <bean id ="student2" class="com.csdn.pojo.Student" p:name="lily" p:age=" 20" p:email="234@qq.com"/>

四:复杂属性注入

<!--复杂属性注入-->
    <bean id = "types" class="com.csdn.pojo.TypesPro">
        <property name="names">
            <!--情况一:数组注入-->
            <array>
                <value>sophia</value>
                <value>sophia2</value>
                <value>sophia3</value>
            </array>
        </property>

        <property name="list">
            <!--情况二:list注入-->
            <list>
                <ref bean="student_2"></ref>
                <ref bean="student_1"></ref>
            </list>
        </property>

        <property name="maps">
            <!--情况三:map注入-->
            <map>
                <entry key="s001" value-ref="student_1"/>
                <entry key="s002" value-ref="student_2"/>
            </map>
        </property>

    </bean>

    <bean id = "student_1" class="com.csdn.pojo.Student">
        <property name="name" value="mike"/>
        <property name="age" value="30"/>
        <property name="email" value="mike@qq.com"/>

    </bean>
    <bean id = "student_2" class="com.csdn.pojo.Student">
        <property name="name" value="mike2"/>
        <property name="age" value="303"/>
        <property name="email" value="mike2@qq.com"/>
    </bean>
package com.csdn.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class TypesPro {
    private String[] names;
    private List<Student> list;
    private Map<String,Student> maps;

    public TypesPro() {
    }

    public TypesPro(String[] names, List<Student> list, Map<String, Student> maps) {
        this.names = names;
        this.list = list;
        this.maps = maps;
    }

    public String[] getNames() {
        return names;
    }

    public void setNames(String[] names) {
        this.names = names;
    }

    public List<Student> getList() {
        return list;
    }

    public void setList(List<Student> list) {
        this.list = list;
    }

    public Map<String, Student> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Student> maps) {
        this.maps = maps;
    }

    @Override
    public String toString() {
        return "TypesPro{" +
                "names='" + Arrays.toString(names) + '\'' +
                ", list=" + list +
                ", maps=" + maps +
                '}';
    }
}

五:举例:用原始方式连接mysql数据库

    <!--要让spring取值,就必须先加载配置文件-->
    <context:property-placeholder location="classpath*:db.properties"/>
    <!--通过property给此bean赋值-->
    <bean id = "jdbcUtil" class="com.csdn.utils.JdbcUtil">
        <property name="driver" value="${db.driver}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>
package com.csdn.utils;

import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcUtil {
    private static String driver;
    private static String username;
    private static String password;
    private static String url;

    public JdbcUtil() {
    }

    public static void setDriver(String driver) {
        JdbcUtil.driver = driver;
    }

    public static void setUsername(String username) {
        JdbcUtil.username = username;
    }

    public static void setPassword(String password) {
        JdbcUtil.password = password;
    }

    public static void setUrl(String url) {
        JdbcUtil.url = url;
    }

    public static Connection getConnection() {
        try {
            Class.forName(driver);
            return (Connection) DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void close(){
        Connection connection = getConnection();
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
    @Test
    public void test04(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object jdbcUtils = ac.getBean("jdbcUtil");
        Connection connection = JdbcUtil.getConnection();
        System.out.println(connection);

        JdbcUtil.close();

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值