美文网首页
注解开发

注解开发

作者: AAnna珠 | 来源:发表于2019-04-25 18:55 被阅读0次

注解开发1

配置文件

<?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.neuedu.demo1"></context:component-scan>

</beans>

service注解

package com.neuedu.demo1;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

//@Component(value="userService")
//<bean id="userService" class="com.neuedu.demo1.UserService" 
//      init-method="setup" destroy-method="teardown" scope="">
//  <property name="info" value="neusoft"></property>
//  <property name="userDao" ref="userDao"></property>
//</bean>
@Service("userService")
@Scope(value="singleton")
public class UserService {
    
    @Value("neusoft")
    private String info;
    
    @Autowired(自动匹配)
    @Qualifier(value="userDao")(指向哪个引用对象)
//  @Resource(name="userDao")
    private UserDao userDao;
    
    public void sayHello(){
        System.out.println("Hello spring ioc annotation"+"--"+info+"--"+userDao);
    }
    
    @PostConstruct
    public void setup(){
        System.out.println("初始化方法。。");
    }
    
    @PreDestroy
    public void teardown(){
        System.out.println("销毁方法。。");
    }
}

service注解开发时的引用对象代码

package com.neuedu.demo1;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//@Component
@Repository("userDao")
//<bean id="userDao" class="com.neuedu.demo1.UserDao">
public class UserDao {

}

测试类

package com.neuedu.demo1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest1 {

    @Test
    public void demo1(){
        ClassPathXmlApplicationContext applicationContext = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = 
                (UserService) applicationContext.getBean("userService");
//      userService.sayHello();
        UserService userService1 = 
                (UserService) applicationContext.getBean("userService");
        System.out.println(userService);
        System.out.println(userService1);
        applicationContext.close();
    }
}

注解开发2

配置文件

<?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">

    <bean id="userService" class="com.neuedu.demo2.UserService"></bean>

</beans>

service类

package com.neuedu.demo2;

public class UserService {

    public void sayHello(){
        System.out.println("Hello Junit...");
    }
}

测试类

package com.neuedu.demo2;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext2.xml")
public class SpringTest2 {

    @Autowired
    @Qualifier("userService")
//  @Resource(name="userService")
    private UserService userService;
    
    @Test
    public void demo1(){
        userService.sayHello();
    }
}

相关文章

  • 注解开发

    注解开发1 配置文件 service注解 service注解开发时的引用对象代码 测试类 注解开发2 配置文件 s...

  • 七、Spring的Bean管理XML方式与注解方式比较

    XML 和注解: XML :结构清晰. 注解 :开发方便.(属性注入.)实际开发中还有一种 XML和注解结合开发:...

  • Android 注解检测、限制操作

    Android 开发中资源常量限制注解: Android开发中权限申请提示注解: 数值常量注解:

  • 105、【JavaEE】【Mybatis】注解开发

    1、概述 使用注解开发,就不需创建相应的映射配置文件。 注解开发、映射配置文件开发各有优劣。注解开发和 XML 配...

  • SSH框架之Spring进阶IOC注解开发(二)

    第一节:IOC的注解开发 1.1 Spring注解开发入门 在Spring4的版本中,若使用注解除了引入开发包以为...

  • Mybatis高级阶段

    Mybatis基于注解开发 这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编...

  • Spring-基础使用

    零、本文纲要 一、Spring基础 相关依赖 xml文件开发 半注解开发 纯注解开发 一、Spring基础 1. ...

  • spring mvc 父子容器

    看本文之前首先需了解 spring注解驱动开发。可以看另外一篇文章spring注解,本文是在spring注解开发的...

  • Java注解

    什么是注解 注解对于开发人员来讲既熟悉又陌生,熟悉是因为只要你是做开发,都会用到注解(常见的@Override);...

  • 深入浅出Java注解

    什么是注解 注解对于开发人员来讲既熟悉又陌生,熟悉是因为只要你是做开发,都会用到注解(常见的@Override);...

网友评论

      本文标题:注解开发

      本文链接:https://www.haomeiwen.com/subject/daqtnqtx.html