Spring_IOC注解方式装配Bean对象

来源:互联网 发布:php二次开发有前途没 编辑:程序博客网 时间:2024/05/17 01:52

1.配置注解的优点:

1.利用反射机制,获取配置信息,从而减少xml的配置工作。

          2.配置信息和java代码在同一个文件中,程序员在开发中不需要频繁的切换,这种方式在思维上提高了开发的效率,这种方式也提高了代码的内聚性。

2.spring配置bean对象的注解介绍:

Spring2.5 引入使用注解去定义Bean

@Component  描述Spring框架中Bean

 

Spring的框架中提供了与@Component注解等效的三个注解:

@Repository 用于对DAO实现类进行标注

@Service 用于对Service实现类进行标注

@Controller 用于对Controller实现类进行标注

三个注解为了后续版本进行增强的.

3.入门程序

框架:

第一步导入jar包:

1.spring-core.jar这个jar 文件包含Spring 框架基本的核心工具类。Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。外部依赖Commons Logging, (Log4J)。

2.spring-beans.jar这个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件就可以了。外部依赖spring-core,(CGLIB)。

3.spring-context.jar这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。

外部依赖spring-beans, (spring-aop)。

4.spring-expression-3.2.2.jar 支持spring表达式语言。

5.commons-logging-1.2.jar日志的jar包。

第二步写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.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">



<!-- 让注解起作用 -->
<context:annotation-config></context:annotation-config>

<!-- 自动扫描这个包下的所有注解 -->
<context:component-scan base-package="com.zhangyike.zhujie"></context:component-scan>

</beans>


第三步编写java文件


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


//该注解相当于aplicationContext.xml中的一个bean配置
@Component
public class User {
//value的注解相当于给uname属性赋值为“张一柯”,在加载spring的时候就把属性注入进去了。
@Value("张一柯")
private String uname;

//value的注解相当于给age属性赋值为“20”,在加载spring的时候就把属性注入进去了。
@Value("20")
private int age;

public String getUname() {
return uname;
}


public void setUname(String uname) {
this.uname = uname;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


@Override
public String toString() {
return "User [uname=" + uname + ", age=" + age + "]";
}
}


import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//该注解相当于aplicationContext.xml中的一个bean配置
@Service
public class UserService {
//该注解相当于自动按类型给user属性注入值
@Autowired
private User aser;

public UserService(){
System.out.println("我是构造方法!");
}

public void getUser(){
System.out.println(aser);
}

//构造方法之后运行
@PostConstruct
public void start(){
System.out.println("开始运行");
}
//关闭资源文件后执行
@PreDestroy
public void destory(){
System.out.println("结束");
}
}

第四步编写测试类

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

//按照注解方式得到对象的时候,这个名字要是类名的第一个字母小写
//获取对象的方法1
UserService userService = (UserService) applicationContext.getBean("userService");
//获取对象的方法2,不需要强转
UserService userService1 = applicationContext.getBean("userService",UserService.class);

userService.getUser();

applicationContext.close();
}
}

总结

1.本文章主要介绍了用spring框架注解的方式获取bean对象,该方法省去了配置xml,直接在java文件中用注解的方式去声明,提高了编程效率,可谓是一个简单的方式,但是在实际的开发应用中常常采用xml和注解方式结合的方式,比如在用到jdbc需要数据源的时候,最好采用xml的方式,如果用注解的方式,每次要修改数据源的时候都要修改java文件,无疑降低了开发效率。

2.了解每个注解的作用,具体用在哪个层,不要都用@Component。

        3.配置文件一定要写在source Folder建的文件夹下,不然用这种方式是找不到的,配置文件在加载的时候,名称也要写到。

4.导入jar包的时候,一定要明白哪个jar包是什么意思,不是全导或者乱导,这样会造成的错误很难发现。


0 0
原创粉丝点击