#自动装配的小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 基础很重要!

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

Spring基于的注解自动装配和依赖注入(***)的更多相关文章

  1. Spring的自动装配与依赖注入

    Spring的自动装配与依赖注入 装配 = 创建Bean + 注入Bean 创建Bean 自动发现 显式注册Bean 注入Bean 基于配置的注入 自动注入 Spring的装配分为显式装配和隐式装配, ...

  2. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  3. Spring自动装配之依赖注入(DI)

    依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...

  4. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring4笔记5--基于注解的DI(依赖注入)

    基于注解的DI(依赖注入): 对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例.只需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解. < ...

  6. Spring升级案例之IOC介绍和依赖注入

    Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...

  7. 三大框架 之 Spring(IOC控制反转、DI依赖注入)

    目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...

  8. Spring IOC源代码具体解释之容器依赖注入

    Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...

  9. Spring详解(三)------DI依赖注入

    上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...

随机推荐

  1. 71A

    #include <iostream> #include <string> using namespace std; int main() { string word; int ...

  2. iot平台在k8s搭建过程

    统一在   cd /opt/iot nohup /opt/iopservices.sh >/var/log/helmapi.log & 直接查看pod日志? kubectl logs i ...

  3. k8s pv 的三种挂载模式

    ReadWriteOnce:可读可写,只能被一个Node节点挂载 ReadWriteMany:可读可写,可以被多个Node节点挂载 ReadOnlyMany:只读,能被多个Node节点挂载

  4. 利用TensorFlow实现线性回归模型

    准备数据: import numpy as np import tensorflow as tf import matplotlib.pylot as plt # 随机生成1000个点,围绕在y=0. ...

  5. MySQL数据类型--与MySQL零距离接触2-8查看数据表

    SHOW COLUMNS FROM tb_name 写入列之后,需要写入行,也就是记录:INSERT 插入记录:INSERT [INTO]  tbl_name  [(col_name,...)]  V ...

  6. Linux基础(六) Vim之vundle插件

    背景 Vim缺乏默认的插件管理器,所有插件的文件都散布在~/.vim下的几个文件夹中,插件的安装与更新与删除都需要自己手动来,既麻烦费事,又可能出现错误. Vundle简介 Vundle 是 Vim ...

  7. gitlab数据迁移至其他gitlb服务器上

    需求: A : 待迁移服务器,上边存有数据 B:接收项目得服务器,本身存在数据 验证方案: 一,搭建gitlab8.15.2 OS:rhel7.4 yum install policycoreutil ...

  8. C#日期格式字符串的相互转换

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...

  9. C#中换行的代码

    1.Windows 中的换行符"\r\n"2.Unix/Linux 平台换行符是 "\n".3.MessageBox.Show() 的换行符为 "\n ...

  10. 关于toolchain(工具链)的一点知识

    之前一直觉得toolchain是个高大上的东西,现摘录 uClibc中的FAQ以助理解. A toolchain consists of GNU binutils, the gcc compiler, ...