zoukankan      html  css  js  c++  java
  • spring注解

    相关文章:http://hi.baidu.com/iduany/item/37ba4b3a833ffcc1382ffa70,关键是简单明了


    这篇文章挺管用的 

    下面简单的列出实现注解的代码:

    springtest.java

    public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
    productCreator pe=(productCreator)ctx.getBean("productCreator");
    pe.execPdao();

     }

    productCreator.java

    public class productCreator {
    @Resource
    public personDao pDao=null;//注入personDao类
    public productCreator(){
    System.out.println("........正在被创建!");
    }
    public static personDao createPersonDao(){
    return new personDao();
    }

    /****很奇怪,引入下面这段代码会报错,真不知道是什么原因*****/ 

    // public personDao productAcreate(){
    // return new personDao();
    // }
    public void execPdao(){
    pDao.Test();
    }

     personDao,java

    public void Test(){
    System.out.println("hello,spring");
    }

     

    bean.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"
    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">
        <context:annotation-config/>
    <!--  
    直接配置javabean
    <bean id="persondao" class="springdao.personDao" scope="prototype">
    </bean>
    -->
    <!--  通过静态factory配置javabean
    <bean id="productCreator" class="serviceImpl.productCreator" factory-method="createPersonDao">
    </bean>
    -->
    <bean id="productCreator" class="serviceImpl.productCreator" scope="prototype"/>
    <bean id="productAcreate" factory-bean="productCreator" factory-method="productAcreate" scope="singleton" lazy-init="true"/> 
    <bean id="personDao" class="springdao.personDao"/>
    </beans>

    运行结果: 

    ........正在被创建!
    hello,spring
  • 相关阅读:
    转载:quartz详解:quartz由浅入深
    git提交忽略文件或文件夹
    Spring学习笔记(一)
    转载:RabbitMQ常用命令
    linux安装rabbitMQ
    linux安装redis
    springMVC+spring+mybatis多数据源配置
    (二)RabbitMQ使用笔记
    ASP.NET Core 异常处理与日志记录
    ASP.NET Core中间件实现分布式 Session
  • 原文地址:https://www.cnblogs.com/php321/p/3393802.html
Copyright © 2011-2022 走看看