java Spring 基于注解的配置(一)

注解引用:1.service.xml 配置注解模式

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!--将针对注解的处理器配置好  -->
<context:annotation-config /> 

<context:component-scan base-package="*.*"/>
</beans>

2.UserService.java

使用@Service 命名该类的name

使用@Controller("userService") 效果也是一样

如果单使用@Controller 那么命名就只能用userService 这样的驼峰

package com.sun.service;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userservice")
public class UserService {
    private String name;
    private ArrayList arr;
        public ArrayList getArr() {
        return arr;
    }
    public void setArr(ArrayList arr) {
        this.arr = arr;
    }
    public UserService(){
        System.out.println("this is chushihua");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void init(){
        System.out.println("init------------");
    }
    public void cleanup(){
            System.out.println("cleanup------------");
    }

}
    

3.HelloWorld.java

package com.sun.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AbstractApplicationContext app = new ClassPathXmlApplicationContext("service.xml");
        UserService us  = (UserService) app.getBean("userservice");
        us.setName("Hello World");
        System.out.println(us.getName());
        //us.setName("sunzhiyan");
        /*System.out.println(us.getName());
        for(int i = 0;i < us.getArr().size();i++){
            System.out.println(us.getArr().get(i));
        }
        app.registerShutdownHook();*/

    }

}

这样能够进行输出。

时间: 2024-08-01 12:24:27

java Spring 基于注解的配置(一)的相关文章

(spring-第4回)spring基于注解的配置

基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样一样的. 一.举个栗子: 1 package com.mesopotamia.annotation; 2 3 import org.springframework.stereotype.Component; 4 5 @Component 6 public class Car { 7 private

spring基于注解和配置源码解读

我们先来建立一个maven项目,引入spring文件,不爱弄的在文章最下面有代码地址可以去下载.先看,后面自己下载代码自己去尝试.先给你们吧,边尝试边看吧. 一.IOC容器注册组件的方式 1. 基础XML注入Bean 是不是超级简单的,我们由浅入深一点点来. 2. 基于注解的方式来配置 我们通过方法名就可以直接得到我们的对象了,默认就是按照方法来装配.也可以通过@Bean(value="newName") 来指定装配的名字. 3. 按照包扫描的方式装配(重点),使用@Component

Spring基于注解的配置之@Autowired

学习地址:https://www.w3cschool.cn/wkspring/rw2h1mmj.html @Autowired注释 @Autowired注释对在哪里和如何完成自动连接提供了更多的细微控制. Setter方法中的@Autowired SpellChecker.java: package com.lee.autowired; public class SpellChecker { public SpellChecker() { System.out.println("Inside S

【Spring】IOC之基于注解的配置bean

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.基于注解的配置 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这3 个注

Spring框架bean的配置(3):基于注解的配置,Autowired 自动装配 Bean,泛型依赖注入

1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Controller: 标识表现层组件 建立接口:UserRepository package com.atguigu.spring.beans.annotation.test; public interface UserRepository { void save(); } 建立类:UserReposito

阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置这里就可以删除了 配置注解 使用@Service注解 开始AOP配置 把通知类交给Spring来管理 在Logger上加注解.之类注意,@Service和@Repository都不合适.因为logger属于三层 所以这里用@Component这个注解来配置 写完上面的@Component的注解后.b

从源码分析 Spring 基于注解的事物

在spring引入基于注解的事物(@Transactional)之前,我们一般都是如下这样进行拦截事物的配置: <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation=&q

spring基于注解的IOC(2)

spring第二天:spring基于注解的IOC以及IoC的案例1.spring中ioc的常用注解 用于创建对象的:Component.Controller.Service.Repository 用于注入数据的:Autowired.Qualifier.Resource.Value 用于改变作用范围的:Scope . 和生命周期相关:PreDestroy .PostConstruct 2.案例使用xml方式和注解方式实现单表的CRUD操作 持久层技术选择:dbutils3.改造基于注解的ioc案例

结合项目(Spring+(基于注解的)SpringMVC和Mybatis+uploadify文件上传)--poi解析Excel文件

poi解析Excel文件 1.上传文件至服务器 2.解析Excel文件并返回数据集合 3.将数据保存到服务器 框架======Spring+(基于注解的)SpringMVC和Mybatis===== 第一步: 前台: jsp文件采用的是uploadify <div id="fileQueue"></div> <input type="file" id="brandFile"> js: <script ty