EricGu's Java-Record-Space

专注 学习 实践 创新

     摘要: 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO 的属性名、类型等信息,如果关系表字段和 PO 属性名、类型都一致,您甚至无需编写任务属性映射信息——因为这些信息都可以通过 Java 反射机制获取。 注释和 Java 代码位于一个...  阅读全文
posted @ 2009-03-11 17:12 Eric Gu 阅读(503) | 评论 (0)编辑 收藏

研究了很久新出的 Spring 2.5, 总算大致明白了如何用标注定义 Bean, 但是如何定义和注入类型为 java.lang.String 的 bean 仍然未解决, 希望得到高人帮助.

总的来看 Java EE 5 的标注开发方式开来是得到了大家的认可了.

@Service 相当于定义 bean, 自动根据 bean 的类名生成一个首字母小写的 bean

@Autowired 则是自动注入依赖的类, 它会在类路径中找成员对应的类/接口的实现类, 如果找到多个, 需要用 @Qualifier("chineseMan") 来指定对应的 bean 的 ID.

一定程度上大大简化了代码的编写, 例如一对一的 bean 映射现在完全不需要写任何额外的 bean 定义了.

下面是代码的运行结果:

man.sayHello()=你好
SimpleMan said: Hi
org.example.EnglishMan@12bcd4b said: Hello

 

代码:

beans.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/>
<context:component-scan base-package="org.example"/>
</beans>

测试类:

import org.example.IMan;
import org.example.SimpleMan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        SimpleMan dao = (SimpleMan) ctx.getBean("simpleMan");
        System.out.println(dao.hello());
        IMan man = (IMan) ctx.getBean("usMan");
        System.out.println(man.sayHello());
    }
}

自动探测和注入bean的类:

package org.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class SimpleMan {
    // 自动注入名称为 Man 的 Bean
    @Autowired(required = false)
    @Qualifier("chineseMan")
    //@Qualifier("usMan")
    private IMan man;   

    /**
     * @return the man
     */
    public IMan getMan() {
        return man;
    }

    /**
     * @param man the man to set
     */
    public void setMan(IMan man) {
        this.man = man;
    }

    public String hello() {
        System.out.println("man.sayHello()=" + man.sayHello());
        return "SimpleMan said: Hi";
    }
}

 

一个接口和两个实现类:

package org.example;

/**
* 抽象的人接口.
* @author BeanSoft
* @version 1.0
*/
public interface IMan {
    /**
     * 打招呼的抽象定义.
     * @return 招呼的内容字符串
     */
    public String sayHello();
}

 

package org.example;

import org.springframework.stereotype.Service;

/**
* 中国人的实现.
* @author BeanSoft
*/
@Service
public class ChineseMan implements IMan {

    public String sayHello() {
        return "你好";
    }

}

 

package org.example;

import org.springframework.stereotype.Service;

/**
* @author BeanSoft
*
*/
@Service("usMan")
// 这里定义了一个 id 为 usMan 的 Bean, 标注里面的属性是 bean 的 id
public class EnglishMan implements IMan {

    public String sayHello() {
        return this + " said: Hello!";
    }

}

posted @ 2009-03-11 16:56 Eric Gu 阅读(208) | 评论 (0)编辑 收藏

IoCDI

  首先想说说IoCInversion of Control,控制倒转)。这是spring的核心,贯穿始终。所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系。这是什么意思呢,举个简单的例子,我们是如何找女朋友的?常见的情况是,我们到处去看哪里有长得漂亮身材又好的mm,然后打听她们的兴趣爱好、qq号、电话号、ip号、iq………,想办法认识她们,投其所好送其所要,然后嘿嘿……这个过程是复杂深奥的,我们必须自己设计和面对每个环节。传统的程序开发也是如此,在一个对象中,如果要使用另外的对象,就必须得到它(自己new一个,或者从JNDI中查询一个),使用完之后还要将对象销毁(比Connection等),对象始终会和其他的接口或类藕合起来。

  那么IoC是如何做的呢?有点像通过婚介找女朋友,在我和女朋友之间引入了一个第三者:婚姻介绍所。婚介管理了很多男男女女的资料,我可以向婚介提出一个列表,告诉它我想找个什么样的女朋友,比如长得像李嘉欣,身材像林熙雷,唱歌像周杰伦,速度像卡洛斯,技术像齐达内之类的,然后婚介就会按照我们的要求,提供一个mm,我们只需要去和她谈恋爱、结婚就行了。简单明了,如果婚介给我们的人选不符合要求,我们就会抛出异常。整个过程不再由我自己控制,而是有婚介这样一个类似容器的机构来控制。Spring所倡导的开发方式就是如此,所有的类都会在spring容器中登记,告诉spring你是个什么东西,你需要什么东西,然后spring会在系统运行到适当的时候,把你要的东西主动给你,同时也把你交给其他需要你的东西。所有的类的创建、销毁都由 spring来控制,也就是说控制对象生存周期的不再是引用它的对象,而是spring。对于某个具体的对象而言,以前是它控制其他对象,现在是所有对象都被spring控制,所以这叫控制反转。如果你还不明白的话,我决定放弃。

IoC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DIDependency Injection,依赖注入)来实现的。比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了 spring我们就只需要告诉springA中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,spring会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制。A需要依赖 Connection才能正常运行,而这个Connection是由spring注入到A中的,依赖注入的名字就这么来的。那么DI是如何实现的呢? Java 1.3之后一个重要特征是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,spring就是通过反射来实现注入的。关于反射的相关资料请查阅java doc


    我想通过Bromon的介绍,大家对IoCDI都有了比较生动的理解了。再来看看《expert one-on-one J2EE Development without EJB中文版》是怎么解释这两个概念的。书上是这么说的:

IoC是一个很大的概念,可以用不同的方式来实现。主要的实现形式有两种:

依赖查找:容器提供回调接口和上下文环境给组件。EJBApache Avalon都是使用这种方式。

依赖注入:组件不做定位查询,只是提供普通的Java方法让容器去决定依赖关系。容器全权负责组件的装配,它会把符合依赖关系的对象通过JavaBean属性或者构造子传递给需要的对象。通过JavaBean属性注射依赖关系的做法称为设值方法注入(Setter Injection);将依赖关系作为构造子参数传入的做法称为构造子注入(Constructor Injection)。

附图说明:


spring_2.jpg

   

到这里,大家应该对IoCDI都有了初步的认识了。其实就Spring来说,就是JavaBeanSpring来管理组装,表面上看就少了几个new字,其实就是为了降低耦合度,这也是我们做软件的目标之一。

至于Spring是怎样实现IoC的,expert one-on-one J2EE Development without EJB中文版》第七章“Spring框架介绍”很详细的列举了多种方法。说实在,一下子看这么多,我真有点糊涂了。我还是自己写个Demo熟悉一下大名鼎鼎的Spring吧。

首先得下载SpringSpring网上有两种Spring 包一种是spring-framework-1.2.6-with-dependencies.zip,另一种是spring-framework-1.2.6.zip。当然最好是下载spring-framework-1.2.6-with-dependencies.zip形式的,因为里面包括了更多的东东。spring-framework-1.2.6-with-dependencies.zip的下载地址是:http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.6-with-dependencies.zip

下载下来,解压后,你会发现里面有很多jar文件。因为刚刚接触Spring,因此我只需要spring-core.jarspring-framework-1.2.6"dist),将其导入eclipse的构建路径中。另外,log日志是需要的,这也是为了养成良好的编程习惯。将log4j-1.2.9.jarspring-framework-1.2.6"lib"log4j)和commons-logging.jarspring-framework-1.2.6"lib"jakarta-commons)导入到构建路径中。

准备就绪,开始写Demo了。

我的工程的结构是:

   spring_1.jpg

<!--[if !supportLists]-->1、<!--[endif]-->log4j.properties代码:

log4j.rootLogger=Debug, stdout
log4j.appender.stdout
=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=%c{1- %m%n

如何使用Log4j,请看我的另一篇转贴的文章:如何使用Log4J

<!--[if !supportLists]-->2、<!--[endif]-->HelloBean的代码:

package com;

public class HelloBean {
    
private String helloworld="Hello!World!";
    
    
public String getHelloworld() {
        
return helloworld;
    }
    
    
public void setHelloworld(String helloworld) {
        
this.helloworld = helloworld;
    }
}

这是一个简单的JavaBean,有个String类型的helloworld属性,初始值是"Hello!World!" 

<!--[if !supportLists]-->3、<!--[endif]-->Bean.xml代码:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
    
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   
<bean id="helloBean" class="com.HelloBean">
        
<property name="helloworld">
            
<value>Hello!Rick</value>
        
</property>
   
</bean>
</beans>

        Spirng重点之一就是配置文件,上面是个相当简单的配置文件,我想大家都应该看得懂。最后就是写应用程序了。

4、<!--[endif]-->Test的代码:

package com;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

    
public static void main(String[] args) {
        
//实例化JavaBean,主要是为了比较new对象和依赖注入两者的区别
        HelloBean hellobean=new HelloBean();
        System.out.println(hellobean.getHelloworld());
        
        
//通过Spring访问JavaBean组件
        Resource resource=new ClassPathResource("com/bean.xml");
        BeanFactory factory
=new XmlBeanFactory(resource);
        hellobean
=(HelloBean)factory.getBean("helloBean");
        System.out.println(hellobean.getHelloworld());
    }
}
    这个Demo很好的阐述了Spring的Ioc,其实就Spring而言,就是通过配置文件,让Spring如同一个管家一样来管理所有的Bean类。

    Spring的依赖注入相对复杂一点,主要是明白调用别的Bean,不是通过实例化对象来调用,而是告诉Spring,我需要什么Bean,然后Spring再向你的Bean里面注入你所需要的Bean对象。
    接下来说说代码实现,我只是在刚刚的例子上再添加一点东东。
    首先要增加一个HelloImp的接口,这是问什么呢,那你得问Spring,它定的规矩:JavaBean的实现要有两个部分,一个接口,一个默认实现。你不照做就不行。
    HelloImp代码:  
     
package com;

public interface HelloImp {
    
public void getName();
}

   
    实现HelloImp的Hello代码:
  
package com;

public class Hello implements HelloImp {
    
public void getName(){
        System.out.println(
"Jack");
    }
}
         
    接着就是在HelloBean中调用Hello了。Spring的不同之处也在这体现出来。
package com;

public class HelloBean {
    
private String helloworld="Hello!World!";
    
private HelloImp hello;  //注意这个私有对象是借口

    
public String getHelloworld() {
        
return helloworld;
    }
    
    
public void setHelloworld(String helloworld) {
        
this.helloworld = helloworld;
    }
    
    
public void setHello(HelloImp hello) {
        
this.hello = hello;
    }
    
  
public void get(){
        
this
.hello.getName();
   }

}
    注意字体加粗的地方。

    配置文件也需要增加一点东西:

   
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
    
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!—注意引用的类是具体的类Hello-->
   
<bean id="myHello" class="com.Hello">
   
</bean>
   
<bean id="helloBean" class="com.HelloBean">
        
<property name="helloworld">
            
<value>Hello!Rick</value>
        
</property>
       
<property name="hello">
           
<ref bean="myHello"></ref>
       
</property>

   
</bean>
</beans>

    注意字体加粗的部分。

    最后在Test中添加一句hellobean.get();就可以看到结果了。

package com;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

    
public static void main(String[] args) {
        HelloBean hellobean
=new HelloBean();
        System.out.println(hellobean.getHelloworld());
        
        Resource resource
=new ClassPathResource("com/bean.xml");
        BeanFactory factory
=new XmlBeanFactory(resource);
        
        hellobean
=(HelloBean)factory.getBean("helloBean");
        System.out.println(hellobean.getHelloworld());
        hellobean.get();
    }
}

到这,Spring的IoC和DI总算有了一定的认识,也算是敲开了Spring的大门了。
posted @ 2009-03-11 11:06 Eric Gu 阅读(169) | 评论 (0)编辑 收藏
仅列出标题
共2页: 上一页 1 2 

导航

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜