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       -- 作用在持久层
时间: 2024-08-26 19:59:40

4.Spring注解方式IOC的快速入门的相关文章

spring 注解方式配置Bean

概要: 再classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定组件包括: @Component:基本注解,标示了一个受Spring管理的组件(可以混用,spring还无法识别具体是哪一层) @Respository:建议标识持久层组件(可以混用,spring还无法识别具体是哪一层) @Service:建议标识服务层(业务层)组件(可以混用,spring还无法识别具体是哪一层) @Con

【Quartz】基于Spring注解方式配置Quartz

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka         在上讲[Quartz]Spring3.2.9+Quqrtz2.2.1实现定时实例中,我们使用了XML的方式来配置Quartz定时任务,虽然比用API的方式简便多了,但是Spring还支持基本注解的方式来配置.这样做不仅更加简单,而且代码量也更加少了. 新建一个Java工程,导入要用到的包,Spring3.2.Quartz2.2.1.aopalliance-1.0.jar.co

Spring 注解方式实现 事务管理

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation

使用Spring注解方式管理事务与传播行为详解

使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional注解声明PersonServiceBean底下所有的业务方法需要事务管理,那么事务是如何来管理的呢? 我们知道当每个业务方法执行的时候,它都会打开事务,在业务方法执行结束之后,它就会结束事务.那么它什么时候决定这个事务提交,什么时候决定这个事务回滚呢?原先我们手工控制事务的时候,通常这个事务的提交或回滚是由我们来操纵的,那现在我们采用容器的声明式事务管理,那我们如何知道事务什么时候提交,什么时候回滚呢?答案是:Spr

spring注解方式实现DI和IOC

注解复习: 1.注解就是为了说明java中的某一个部分的作用(Type) 2.注解都可以用于哪个部门是@Target注解起的作用 3.注解可以标注在ElementType枚举类所指定的位置上 4. 元注解 @Documented    //该注解是否出现在帮助文档中 @Retention(RetentionPolicy.RUNTIME) //该注解在java,class和运行时都起作用 @Target(ElementType.ANNOTATION_TYPE)//该注解只能用于注解上 public

Spring 注解方式 实现 IOC 和 DI

注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.tedu.beans.Person; public class IOC

Spring注解方式配置说明

1.<context:annotation-config/>与<context:component-scan base-package=”XX.XX”/> 在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor.CommonAnnotationBeanPostProcessor.Persi

spring注解方式AOP

引入相关依赖:   <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>3.0.5.RELEASE</version>        </dependency>        <dependency&

spring 注解方式 事务管理

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation