【核心核心】4.Spring【IOC】注解方式
1.导入jar包

2.创建对应的类
public interface HelloService {
public void sayHello();
}
/**
* @Component(value="helloService") 相当于 <bean id="helloService" class="com.spring.demo1.HelloSeviceImpl"/>
* @author NEWHOM
*
*/
@Component(value="helloService")
public class HelloSeviceImpl implements HelloService {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello Spring !!");
}
}
3.在applicationContext.xml中引入约束
<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">
</beans>
4.在applicationContext.xml中开启组件扫描
<context:component-scan base-package="com.spring.demo1" />
5.在HelloServiceImpl上添加注解
@Component(value="helloService") 相当于 <bean id="helloService" class="com.spring.demo1.HelloSeviceImpl"/>
6.编写测试类
public class Demo1 {
/**
* 测试注解方式的IOC
*/
@Test
public void m01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) ac.getBean("helloService");
helloService.sayHello();
}
}

注意:
Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
* @Controller -- 作用在WEB层
* @Service -- 作用在业务层
* @Repository -- 作用在持久层
【核心核心】4.Spring【IOC】注解方式的更多相关文章
- spring IOC注解方式详解
本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一,概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以 ...
- 小马哥讲Spring栈核心编程思想 Spring IoC+Bean+Framework
小马哥出手的Spring栈核心编程思想课程,可以说是非常专业和权威的Spring课程.课程主要的方向与核心是Spring Framework总览,带领同学们重新认识重新认识IoC,Spring IoC ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- Spring源码 05 IOC 注解方式
参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...
- Spring中注解方式实现IOC和AOP
1.IOC注解 1.1 IOC和DI的注解 IOC: @Component:实现Bean组件的定义 @Repository:用于标注DAO类,功能与@Component作用相当 @Service:用 ...
- Spring IOC 注入方式详解 附代码
引言 Spring框架作为优秀的开源框架之一,深受各大Java开发者的追捧,相信对于大家来说并不陌生,Spring之所以这么流行,少不了他的两大核心技术IOC和IOP.我们这里重点讲述Spring框架 ...
- Spring IOC 实现方式
Spring 中的 org.springframework.beans 包和 org.springframework.context 包构成了 Spring 框架 IoC 容器的基础. BeanFac ...
随机推荐
- Qt Creator配置
1.安装Git sudo apt install git 2.配置Git 用户和邮箱: git config --global user.name "xxx" git config ...
- java变量和数据类型
变量 数据类型 变量名 = 数据值: 注意事项: 变量定义后可以不赋值,使用时再赋值.不赋值不能使用 变量使用时有作用域的限制. 变量不可以重复定义 数据类型转换 自动类型转换 范围大的数据类 ...
- 线段树离散化+区间更新——cf1179C好题
绝对是很好的题 把问题转化成当第i个询问的答案是数值x时是否可行 要判断值x是否可行,只要再将问题转化成a数组里>=x的值数量是否严格大于b数组里的>=x的值 那么线段树叶子结点维护对于值 ...
- 组合数学——cf1065E
从两端到中间分段,然后累乘即可 #include<bits/stdc++.h> using namespace std; #define mod 998244353 #define max ...
- Hibernate之Inverse的用法
在多的一端配置Inverse设置为true,来自动管理关系
- SQL Server [join] 整理
[表A]Aid Aname Acode1 aa 0012 bb 0023 cc 0034 dd 004 ...
- csp-s模拟测试86
csp-s模拟测试86 分屋前的最后一次考试,我早就放弃了自己. 02:02:46 70 02:02:57 03:16:08 100 03:16:08 $T1$忘了按位计算,达哥按位计算的$T1$当时 ...
- 16.ajax_case09
import requests import json import re from selenium import webdriver from selenium.webdriver.common. ...
- Java基础 ----- 判断对象的类型
1. 判断对象的类型:instanceOf 和 isInstance 或者直接将对象强转给任意一个类型,如果转换成功,则可以确定,如果不成功,在异常提示中可以确定类型 public static vo ...
- 面试系列22 dubbo的工作原理
(1)dubbo工作原理 第一层:service层,接口层,给服务提供者和消费者来实现的 第二层:config层,配置层,主要是对dubbo进行各种配置的 第三层:proxy层,服务代理层,透明生成客 ...