• 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
  • 相关阅读:
    (转)运维角度浅谈MySQL数据库优化
    关于MySQL的null值
    MySQL优化——or条件优化
    MySQL优化原理
    Xcode Archive打包失败问题
    ionic3 对android包进行签名
    ios 审核未通过 相机相册权限问题
    js计算两个日期相差天数
    截取URL链接中字段的方法
    ionic3 自定义组件 滑动选择器 ion-multi-picker
  • 原文地址:https://www.cnblogs.com/php321/p/3393802.html
Copyright © 2020-2023  润新知