使用注解开发
使用注解开发需要引入aop的包
在配置文件当中,还得要引入一个context约束:
<?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">
</beans>
1 Bean的实现
前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解
1.1 配置扫描哪些包下的注解
<!-- 指定要扫描的包,这个包下的注解会生效-->
<context:component-scan base-package="com.lx"/>
1.2 在指定包下编写类,增加注解 组件@Component
@Component 可以指定名字,默认类名首字母小写
@Commonent等价于 <bean id= class= >
1.3 属性注入 @Value
可以不用提供set方法,直接在直接名上添加 @value(“值”) 也可以在set方法上使用
1.3 代码及测试:
实体类:
package com.lx.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等价于 <bean id="user" class="com.lx.pojo.User"/>
// @Component 组件
@Component
public class User {
@Value("lx")
相当于 <property name="name" value="lx"/> 也可以放在set方法上
public String name;
public void setName(String name) {
this.name = name;
}
}
<?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.lx"/>
</beans>
import com.lx.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
}
1.4 衍生注解
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller:web层
@Service:service层
@Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配
2 基于Java类进行配置
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
2.1 @Component
@Component表明这是一个组件,将他放进Spring容器中
实体类:
package com.lx.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("lx")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2.2 JavaConfig
@Configuration 代表这是一个配置类
@Bean 通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id
package com.lx.config;
import com.lx.pojo.User;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan("com.lx.pojo")
public class lxConfig {
@Bean
public User user(){
return new User();
}
}
测试单元:
import com.lx.config.lxConfig;
import com.lx.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Mytest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(lxConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}