其他分享
首页 > 其他分享> > (一)Spring-介绍、对象创建以及依赖注入

(一)Spring-介绍、对象创建以及依赖注入

作者:互联网

(一)Spring-介绍、对象创建以及依赖注入

一、 简介

1.1 Spring的前生

1.2 Spring特征

1.3 Spring的优点

总结:Spring是一个轻量级的控制反转(IoC)面向切面(AOP)的框架

1.4 Spring的组成

1.5 拓展

在Spring的官网有这个介绍:现代化的Java开发!说白了就是基于spring的开发。

因为现在大多数公司都在使用SpringBoot进行快速开发,学习Springboot的前提,需要完全掌握Spring及SpringMVC!承上启下的作用!

Spring Framework的弊端:发展了太久之后,违背了原来的理念!配置十分繁琐,人称配置地狱。

二、IOC理论推导

2.1 IOC之前的原来模式

1 UserDao接口

package com.happy.dao;

import com.happy.pojo.User;

public interface UserDao {

    User getUser();
}

2 UserDaoImpl实现类

package com.happy.dao;

import com.happy.pojo.User;
import com.happy.utils.IDutils;

public class UserDaoMysqlImpl implements UserDao {
    @Override
    public User getUser() {
//        模拟数据库查询到用户了
        User user = new User();
        user.setId(IDutils.getId());
        user.setPwd("123");
        user.setName("happy");

        System.out.println("mysql数据库里获取到user了");
        return user;

    }
}

3 UserService业务接口

package com.happy.service;

import com.happy.dao.UserDao;
import com.happy.pojo.User;

public interface UserService {
//    UserDao userDao=null;
    public User getUser();
}

4 UserServiceImpl业务实现类

注意:

package com.happy.service;

import com.happy.dao.UserDao;
import com.happy.dao.UserDaoImpl;
import com.happy.dao.UserDaoMysqlImpl;
import com.happy.pojo.User;

public class UserServiceImpl implements UserService {
    //    UserDao userDao = new UserDaoImpl();
    //    UserDao userDao = new UserDaoMysqlImpl();
    //    UserDao userDao=new UserDaoImpl()
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User getUser() {
//        UserDao userDao = new UserDaoImpl();

        User user = userDao.getUser();
        return user;
    }
}

5 控制层

package com.happy.servlet;

import com.happy.dao.UserDaoMysqlImpl;
import com.happy.pojo.User;
import com.happy.service.UserService;
import com.happy.service.UserServiceImpl;
import org.junit.Test;

public class TestUserService {

    @Test
    public void testUserService(){
        UserService userService = new UserServiceImpl();
        ((UserServiceImpl) userService).setUserDao(new UserDaoMysqlImpl());
        User user = userService.getUser();
        System.out.println(user);
    }
}

6 小结

在我们之前的业务中,用户的需求变更或者实现方式变更可能会影响我们原来的代码,我们需要根据变动去修改源代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!

我们使用了一个Set接口实现,对外暴露,已经发生了革命性的变化!

这种思想,从本质上解决了问题,我们程序员不用去手动创建和管理对象了,系统的耦合性大大降低,更加专注业务的实现上。这就是IOC的原型!

public class UserServiceImpl implements UserService {
    //    UserDao userDao = new UserDaoImpl();
    //    UserDao userDao = new UserDaoMysqlImpl();
    //    UserDao userDao=new UserDaoImpl()
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

2.2 IOC本质

三、Spring入门

3.1 HelloSpring

1 编写bean

package com.happy.pojo;

public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

2 编写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">

    <bean id="userDaoImpl" class="com.happy.dao.UserDaoImpl"></bean>
    <bean id="userDaoMysqlImpl" class="com.happy.dao.UserDaoMysqlImpl"></bean>
    <bean id="userDaoOracleImpl" class="com.happy.dao.UserDaoOracleImpl"></bean>

    <bean id="userService" class="com.happy.service.UserServiceImpl">
<!--        <property name="userDao" ref="userDaoMysqlImpl"></property>-->
<!--        ref是引用spring容器中创建好的对象-->
        <property name="userDao" ref="userDaoOracleImpl"></property>
    </bean>
</beans>

3 使用bean测试

package com.happy.servlet;

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

public class TestHelloSpring {

    @Test
    public void testHelloSpring(){

//        ClassPathXmlApplicationContext为ApplicationContext的一个实现
//        获取spring的上下文对象!
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Object hello = context.getBean("hello");
        Object hello2 = context.getBean("hello");
        System.out.println(hello);
        System.out.println(hello==hello2);
    }
}

3.2 UserDao的案例

到此,我们继续根据spring来完善第2部分UserDao的案例

1 编写bean

同第二部分1~4部分,编写Service和Dao层的bean

2 编写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">

    <bean id="userDaoImpl" class="com.happy.dao.UserDaoImpl"></bean>
    <bean id="userDaoMysqlImpl" class="com.happy.dao.UserDaoMysqlImpl"></bean>
    <bean id="userDaoOracleImpl" class="com.happy.dao.UserDaoOracleImpl"></bean>

    <bean id="userService" class="com.happy.service.UserServiceImpl">
<!--        <property name="userDao" ref="userDaoMysqlImpl"></property>-->
<!--        ref是引用spring容器中创建好的对象-->
        <property name="userDao" ref="userDaoOracleImpl"></property>
    </bean>
</beans>

3 使用bean测试

package com.happy.servlet;

import com.happy.dao.UserDaoMysqlImpl;
import com.happy.pojo.User;
import com.happy.service.UserService;
import com.happy.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUserService {

    @Test
    public void testUserService(){
        UserService userService = new UserServiceImpl();
        ((UserServiceImpl) userService).setUserDao(new UserDaoMysqlImpl());
        User user = userService.getUser();
        System.out.println(user);
    }


    @Test
    public void testUserServiceBySpring(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("userDao.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        User user = userService.getUser();
        System.out.println(user);
    }
}

四、IOC创建对象方式

4.1 无参构造

默认使用无参构造创建对象,这是默认的。

<bean id="user" class="com.happy.pojo.User"></bean>

4.2 有参构造的3种方式

1 构造器参数下标

    <!--    第一种方式:通过下标index     -->
<bean id="user" class="com.happy.pojo.User">-->
	<constructor-arg index="0" value="gaoyiheng"></constructor-arg>-->
</bean>

2 构造器参数类型

不推荐使用!第二种方式不建议使用,同类型不能精确指定,容易混淆。

<!--    第二种方式:通过下标index     -->
    <!-- 第二种方式不建议使用,同类型不能精确指定,容易混淆-->
<bean id="user" class="com.happy.pojo.User">
      <constructor-arg type="java.lang.String" value="happy518"></constructor-arg>
      <constructor-arg type="java.lang.String" value="happyname"></constructor-arg>
</bean>

3 构造器参数名称

<?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">

    <!--    第三种方式:通过属性的name     -->
    <bean id="user" class="com.happy.pojo.User">
        <constructor-arg name="name" value="gaoxing"></constructor-arg>
        <constructor-arg name="id" value="123"></constructor-arg>
    </bean>
</beans>

五、Spring配置

Spring的配置很少,总共就下面5个一级标签。

5.1 Alias别名

   <bean id="user" class="com.happy.pojo.User">
        <constructor-arg name="name" value="gaoxing"></constructor-arg>
        <constructor-arg name="id" value="123"></constructor-arg>
    </bean>


    <bean id="user2" class="com.happy.pojo.User2" scope="prototype">
    </bean>

    <alias name="user" alias="user1"></alias>
    @Test
    public void testAlias(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
        User user = (User) applicationContext.getBean("user");
        User user1 = (User) applicationContext.getBean("user1");
        System.out.println(user1);
        System.out.println(user==user1);
    }

下图可以看到,两个对象都相等,证明为就是一个别名而已。

另一种起别名的方法

用name

  <bean id="user" class="com.happy.pojo.User" name="user3">
        <constructor-arg name="name" value="gaoxing"></constructor-arg>
        <constructor-arg name="id" value="123"></constructor-arg>
    </bean>

5.2 Bean的配置

1 name

2 scope

指定bean的作用范围。

 <bean id="user" class="com.happy.pojo.User" name="user3">
        <constructor-arg name="name" value="gaoxing"></constructor-arg>
        <constructor-arg name="id" value="123"></constructor-arg>
    </bean>


    <bean id="user2" class="com.happy.pojo.User2" scope="prototype">
    </bean>

5.3 import

<?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">
    <import resource="user.xml"></import>
</beans>

六、依赖注入

6.1 构造器注入

见第4章节。

6.2 set属性注入

1 测试对象bean

package com.happy.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private String name;
    private String wife;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;

}

2 依赖属性-复杂对象bean

package com.happy.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {

    private String Address;
}

3 配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.happy.pojo.Student">
<!--        第一种普通值注入-->
        <property name="name" value="happy"></property>
        <property name="address" ref="addresss"></property>
       <!-- <property name="wife" value="null"></property>-->
        <property name="wife">
            <null></null>
        </property>
        <!--array-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--list-->
        <property name="hobbys">
            <list>
                <value>女</value>
                <value>美女</value>
            </list>
        </property>
        <!--map-->
        <property name="card">
            <map>
                <entry key="信用卡" value="1234"></entry>
                <entry key="储蓄卡" value="4321"></entry>
            </map>
        </property>
        <!--set-->
        <property name="games">
            <set>
                <value>篮球</value>
                <value>足球</value>
            </set>
        </property>
        <!--property-->
        <property name="info">
            <props>
                <prop key="username">happy</prop>
                <prop key="password">12314</prop>
                <prop key="url">jdbc:mysql://localhost/mysql</prop>
            </props>
        </property>
    </bean>
    <!-- 复杂对象-->
    <bean id="addresss" class="com.happy.pojo.Address">
      <property name="address" value="香港"></property>
    </bean>

</beans>

4 测试使用

package com.happy.service;

import com.happy.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Properties;

public class StudentServiceImpl implements StudentService {
    @Override
    public Student getStudent() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        String wife = student.getWife();
        System.out.println(wife);

        Properties info = student.getInfo();
        System.out.println("info的类型:"+info.getClass());
        System.out.println(info.getProperty("url"));
        info.setProperty("addkey","addvalue");
        System.out.println("info:"+info);

        System.out.println(student);
        return student;
    }

    @Test
    public void test() {
        getStudent();
    }
}

6.3 拓展方式注入:P命名空间和C命名空间

我们可以通过P和C命名空间注入依赖

1 p:

p命名空间注入,可以直接注入属性的值:property

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.happy.pojo.User" p:name="happybyP" c:pwd="8888">
       <!-- <property name="name" value="happy518"></property>-->
    </bean>

</beans>

2 c:

c命名空间注入,通过构造器注入:constructor-args

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.happy.pojo.User" c:pwd="8888">
        <property name="name" value="happy518"></property>
    </bean>

</beans>

3 使用测试

package com.happy.service;

import com.happy.pojo.Student;
import com.happy.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Properties;

public class StudentServiceImpl implements StudentService {
    @Override
    public Student getStudent() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        String wife = student.getWife();
        System.out.println(wife);

        Properties info = student.getInfo();
        System.out.println("info的类型:"+info.getClass());
        System.out.println(info.getProperty("url"));
        info.setProperty("addkey","addvalue");
        System.out.println("info:"+info);

        System.out.println(student);
        return student;
    }

    @Test
    public void test() {
        getStudent();
    }

    @Test
    public void testPandC(){
        ApplicationContext context = new ClassPathXmlApplicationContext("user.xml");
        User user = context.getBean("user", User.class);

        System.out.println(user);

    }
}

注意点:p命名和C命名空间不能直接使用,需要导入xml约束。

标签:依赖,Spring,User,创建,import,com,public,happy
来源: https://www.cnblogs.com/happycarpediem/p/16254782.html