zoukankan      html  css  js  c++  java
  • 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();*/
            
        }
    
    }

    这样能够进行输出。

  • 相关阅读:
    (转)js中的hasOwnProperty和isPrototypeOf方法
    backbonejs和requirejs的实例
    判断 iframe 是否加载完成的完美方法(转)
    解决 sublime 的 日常误操作
    动图展示16个Sublime Text快捷键用法 ---------------物化的sublime
    CSS中最合理ID/CLASS的命名规范 —— 绯色的css 系列
    DIV 浮动层 绝对定位居中浮动 用CSS怎么写 —— 绯色的CSS系列
    [转载] CSS样式表IE条件注释(if IE)备忘—— HACK 系列
    [转载]Node.JS平台上的数据库Redis,MongoDB,HBASE,MySQL
    页面加载速度优化的12个建议
  • 原文地址:https://www.cnblogs.com/sunxun/p/5408498.html
Copyright © 2011-2022 走看看