• Spring基于的注解自动装配和依赖注入(***)


    #自动装配的小Demo:

    package
    com.gyf.annotation;
    //DAO层
    public interface UserDao { public void save(); }
    package com.gyf.annotation; import org.springframework.stereotype.Repository; @Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("UserDao接口的实现类的save方法的实现..."); } }

    //业务层
    package com.gyf.annotation; public interface UserService { void save(); } package com.gyf.annotation; import org.springframework.stereotype.Service;
    import javax.annotation.Resource;
    @Service(
    "userService") public class UserServiceImpl implements UserService { @Resource(name = "userDao") private UserDao userDao; @Override public void save() { this.userDao.save(); System.out.println("userservice...save..."); } }

    //控制层
    package com.gyf.annotation;

    import org.springframework.stereotype.Controller;

    import javax.annotation.Resource;

    @Controller("userController")
    public class UserController {
    @Resource(name="userService")
    private UserService userService;
    public void save(){
    this.userService.save();
    System.out.println("userController...save...");
    }
    }

    说明: 

    无论是servlet、JSP,还是ssm都是用的mvc的设计思想;

     Dao层与数据库相关联,业务层并调用DAO层的变量,并进行中间逻辑处理,控制层

    对业务层进行调用,并进行进一步的逻辑控制,包括字符编码的设置等。

    #配置文件

    <?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.xsd"
    > <context:component-scan base-package="com.gyf.annotation"/> </beans>


    package com.gyf.test;
    
    import com.gyf.annotation.UserController;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class test {
        public static void main(String[] args) {
            String xmlPath="beans6.xml";
            ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
            UserController userController=(UserController)ac.getBean("userController");
            userController.save();
        }
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------

    #依赖注入

    //DAO层
    package
    com.gyf.ioc; public interface UserDao { void say(); } package com.gyf.ioc; public class UserDaoImpl implements UserDao { @Override public void say() { System.out.println("userDao say hello world"); } }
    //业务层
    package com.gyf.ioc; public interface UserService { void say(); }
    package com.gyf.ioc; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void say() { this.userDao.say(); System.out.println("userService say hello world"); } }

    #配置文件<?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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
          <bean id="userDao" class="com.gyf.ioc.UserDaoImpl"/>
    
           <bean id="userService" class="com.gyf.ioc.UserServiceImpl">
    <!--将DAO层的数据注入到业务层,说明该方法是set方法进行注入到业务层组件中-->
    <property name="userDao" ref="userDao"/> </bean> </beans>

    #测试:
    package com.gyf.test;
    
    import com.gyf.ioc.UserDao;
    import com.gyf.ioc.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class test {
        @Test
        public void test1(){
            ApplicationContext ac=new ClassPathXmlApplicationContext("beansIOC.xml");
    
            UserDao userDao=(UserDao)ac.getBean("userDao");
            userDao.say();
            UserService userService=(UserService)ac.getBean("userService");
            userService.say();
        }
    }

     学会重复!

    多练习,Spring 基础很重要!

    这个有点类似于多态的运用,以及变量的调用;

    成年人的世界没有那么多的童话,也没有那么多的逆袭。
  • 相关阅读:
    小米手机刷开发版教程(超简单)
    Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]
    git常用命令
    SpringCloud Feign对Hystrix(断路由)的支持
    SpringCloud Feign使用详解
    SpringCloud高可用Eureka搭建
    springCloud Hystrix 断路由
    SpringBoot数据库连接池常用配置
    springBoot actuator监控配置及使用
    springCloud zuul网关服务
  • 原文地址:https://www.cnblogs.com/shijinglu2018/p/10360461.html
Copyright © 2020-2023  润新知