zoukankan      html  css  js  c++  java
  • Spring(1)

    Spring框架:

       什么是Spring框架?

        1.Spring一个开源的,用来简化企业级应用开发的应用开发框架。Spring的核心就是IOC(控制反转)和AOP(面向切面编程);

        2.Spring框架可以理解为就是一个容器,用于管理对象的生命周期;

        3.Spring容器:spring框架中的一个核心模块,用来管理对象(包括对象的创建,销毁和初始化等)。它帮助完成类的初始化与装配工作,让开发者从这些底层类的实例化,依赖关系装配类的工作中脱离出来,专注于更有意义的业务逻辑开发工作;

        4.Spring是一个IOC(DI) 和 AOP 框架

            其中IOC(DI) 称为:控制反转 即把对象的获取方式进行了反转,例子(引用于百度)

            Class A 中用到了Class 的对象B,一般情况下,需要在A的代码中显示只能new 一个B对象,采用依赖注入技术之后,A的代码只需要定义一个私有的B对象,不需要直接new来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new出来并注入到A类里的引用中

     本文聊聊:1.Spring依赖注入的方式

          2.依赖注入的类型

          3.Bean的作用域

             4.自动注入

          5.在Spring配置文件中引入属性文件

             6.使用注解的方式完成

          Spring的 jar 包

    (1)Spring依赖注入的方式

    首先创建一个Hello类:一定要加上set方法,下图没有

     

        1.通过set方法来完成依赖注入

          

    <?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="Hello" class="com.zhiyou100.ydb.spring.Hello">
         <!--<property 就是通过set方法来注入Hello类的name属性的值 -->
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
        </bean>
    </beans>

      2.通过构造方法来完成依赖注入

    <bean id="Hello2" class="com.zhiyou100.ydb.spring.Hello">
        <!-- 
                constructor-arg 构造方法的参数
                index:表示第几个参数 索引从0开始
                相当于 Hello h = new Hello("李四",18);
         -->
        <constructor-arg index="0" value="李四"></constructor-arg>
        <constructor-arg index="1" value="18"></constructor-arg>
    </bean>

      测试:

    package com.zhiyou100.ydb.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zhiyou100.ydb.spring.Hello;
    
    public class HelloTest {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
            Hello h = (Hello) app.getBean("Hello");
            h.Show();
    }

    运行结果:

    (2)依赖注入的数据类型

    基本数据类型和字符串使用 value  如果是指向另一个对象的引用使用ref=

    对象数据类型

    列子:因为要用到另一个对象 (Student类)

    package com.zhiyou100.ydb.spring;
    
    public class Student {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        
    }

    在Hello类中加入Student 属性

     

    <bean id="Hello" class="com.zhiyou100.ydb.spring.Hello">
         <!--<property 就是通过set方法来注入Hello类的name    属性的值 -->
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
        <!-- ref指向另一个bean对象 -->
        <property name="student" ref="stu"></property>
    </bean>
    <bean id="stu" class="com.zhiyou100.ydb.spring.Student">
        <property name="address" value="南京"></property>
    </bean>
    </beans>

    测试:

     集合数据类型

    在Hello类中加入list属性

    <bean id="Hello" class="com.zhiyou100.ydb.spring.Hello">
         <!--<property 就是通过set方法来注入Hello类的name    属性的值 -->
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
        <!-- ref指向另一个bean对象 -->
        <property name="student" ref="stu"></property>
        <property name="list">
            <list>
                <value>刘能</value>
                <value>赵四</value>
                <value>广坤</value>
                <value>宋小宝</value>
            </list>    
        </property>
            </bean>
    </beans>

    测试

    Map数据类型

      在Hello类中加入Map类型

    <bean id="Hello" class="com.zhiyou100.ydb.spring.Hello">
         <!--<property 就是通过set方法来注入Hello类的name    属性的值 -->
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
        <!-- ref指向另一个bean对象 -->
        <property name="student" ref="stu"></property>
        <property name="list">
            <list>
                <value>刘能</value>
                <value>赵四</value>
                <value>广坤</value>
                <value>宋小宝</value>
            </list>    
        </property>
        <property name="map">
            <map>
                <entry key="kobe" value="科比"/>
                <entry key="wade" value="韦德"/>
            </map>
        </property>
    </bean>
    </beans>

    测试:

     (3)Bean的作用域

      Bean 的作用域默认为单例模式

     (4)自动注入

        自动注入该类中的属性

      首先需要创建一个类(UserDao)

    <bean id="udao" class="com.zhiyou100.ydb.spring.UserDao"></bean>
        <bean id="udao1" class="com.zhiyou100.ydb.spring.UserDao"></bean>
        <!--
            autowire:自动注入的属性
            byType:根据userDao属性的类型,找与之匹配的bean
            byName:根据属性名 找与之匹配的id
            no:需要自己手动注入
            default:采用全局的default-autowire设置
         -->
        <bean id="uservice" class="com.zhiyou100.ydb.spring.UserService" autowire="default">
            
        </bean>

    注意:如果使用default 需要在头部加入 default-autowire

    (5)在spring配置文件中引入属性文件

    首先创建外部的属性文件,放入到配置文件夹中

     引入属性文件 使用  context:property-placeholder 标签

    <!-- 引入外部属性文件 -->
        <context:property-placeholder location="classpath:my.properties"/>
        <bean id="user" class="com.zhiyou100.ydb.spring.User">
            <property name="name" value="${user.names}"></property>
            <property name="age" value="${user.age}"></property>
            <property name="sex" value="${user.sex}"></property>
        </bean>
        

    注意:如果引入多个文件可以使用通配符 * 也可以使用逗号分隔引入

    (6)使用注解的方式

      首先不使用注解

    上例子!

    操作层:创建Dao包,里面创建好UserDaoImp 类 以及该类的接口 UserDao

    UserDao 接口

    package com.zhiyou100.ydb.dao;
    
    public interface UserDao {
    
        void SelectById(int id);
    
    }

    UserDaoImp类 实现UserDao接口 

    package com.zhiyou100.ydb.dao;
    
    import org.springframework.stereotype.Repository;
    
    public class UserDaoImp implements UserDao{
        
        @Override
        public void SelectById(int id) {
            System.out.println("根据id查询信息");
        }
    }

    业务层:创建service包,里面创建好UserServiceImp 类 以及该类接口 UserService

    package com.zhiyou100.ydb.service;
    
    public interface UserService {
    
        void QueryById(int id);
    
    }

    UserServiceImp类 实现UserService

    package com.zhiyou100.ydb.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zhiyou100.ydb.dao.UserDao;
    import com.zhiyou100.ydb.dao.UserDaoImp;
    
    public class UserServiceImp implements UserService {
        private UserDao userdao;
    
        public void setUserdao(UserDao userdao) {
            this.userdao = userdao;
        }
        
        @Override
        public void QueryById(int id) {
            userdao.SelectById(id);
        }
    }

    控制层:创建controller包,里面创建好UserControler类

    package com.zhiyou100.ydb.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    
    import com.zhiyou100.ydb.service.UserService;
    
    public class UserController {
        private UserService userService;
        
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    
        public String findById(int id) {
            userService.QueryById(id);
            return "index";
        }
    }

    在配置文件加入相应的类

    <?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">
    <!-- 管理UserDao的实现类 -->
    <bean id="userDao" class="com.zhiyou100.ydb.dao.UserDaoImp"></bean>
    <!-- 管理userService的实现类 -->
    <bean id="userService" class="com.zhiyou100.ydb.service.UserServiceImp">
        <property name="userdao" ref="userDao"></property>
    </bean>
    <!-- 管理UserController的实现类 -->
    <bean id="userController" class="com.zhiyou100.ydb.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>
    </beans>

    测试:

    package com.zhiyou100.ydb.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zhiyou100.ydb.controller.UserController;
    
    public class UserConrollerTest {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserController uc = (UserController) app.getBean("userController");
            uc.findById(1);
        }
    }

    结果成功输出 SelectById

     而使用注解就是在各个类中加入

    @Repository 持久化注解 用在Dao 类上

    @Service 业务层注解

    @ Autowired 自动注入 按照类型自动注入,如果有多个类型相同的那么就会按照名称注入

    @Controller控制层注解

    当然还要加入 aop的jar 包

     然后在各类中加入注解

    Dao类

    package com.zhiyou100.ydb.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository(value="userDao")
    public class UserDaoImp implements UserDao{
        
        @Override
        public void SelectById(int id) {
            System.out.println("根据id查询信息");
        }
    }

    Service类

    package com.zhiyou100.ydb.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zhiyou100.ydb.dao.UserDao;
    import com.zhiyou100.ydb.dao.UserDaoImp;
    @Service(value="userService")
    public class UserServiceImp implements UserService {
        private UserDao userdao;
        @Autowired
        public void setUserdao(UserDao userdao) {
            this.userdao = userdao;
        }
        
        @Override
        public void QueryById(int id) {
            userdao.SelectById(id);
        }
    }

    Controller类

    package com.zhiyou100.ydb.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    
    import com.zhiyou100.ydb.service.UserService;
    @Controller(value="userController")
    public class UserController {
        private UserService userService;
        @Autowired
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    
        public String findById(int id) {
            userService.QueryById(id);
            return "index";
        }
    }

    测试

    package com.zhiyou100.ydb.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zhiyou100.ydb.controller.UserController;
    
    public class UserConrollerTest2 {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
            UserController uc = (UserController) app.getBean("userController");
            uc.findById(1);
        }
    }

    结果也可以正常打印SelectById 方法

    注意:使用注解可以不加set方法哦 但是使用xml 方法必要要加set方法

    上面使用的 @Autowired 注解是可以完成自动注入的功能,但是还有一个注解也可以完成此功能——@Resource 自动注入,按照名称注入,如果没有相同名称的bean那么会按照类型帮你注入。它可以指定名称来注入,如果没有起名,那么它的名称就是属性的名称。

  • 相关阅读:
    Javascript异步编程的4种方法
    同步编程和异步编程
    关于js 异步回调的一些方法
    array的方法 没记住的
    阮一峰关于reduce 和transduce的博客
    CSS开发小技巧
    提升自己的一个网址
    asm.js 和 Emscripten 入门教程
    Koa -- 基于 Node.js 平台的下一代 web 开发框架
    C#中使用handsonetable的一个例子
  • 原文地址:https://www.cnblogs.com/Kuriyama-Mirai/p/11477958.html
Copyright © 2011-2022 走看看