在spring4 之后 使用注解开发 必须保证aop包的导入
使用注解需要导入context的约束,增加注解的支持
1.sring配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/context/spring-aop.xsd">
<!-- 注意: 指定要扫描的包,这个包下的注解就会生效 不写注解容器扫描不到-->
<context:component-scan base-package="com.huahua"/>
<!-- 开启自动装配 -->
<context:annotation-config/>
</beans>
2.实体类 User类:
package com.huahua.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*相当于xml文件中的<bean id="user" class="com.huahua.pojo.User"/>
* id名字默认为类的名字 小写
* */
@Component
public class User {
/* 相当于 <property name="name" value="花花"/>
* */
@Value("花花")
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//也可以放在set方法上
@Value("23")
public void setAge(int age) {
this.age = age;
}
}
3.dao层 :
接口:
package com.huahua.dao;
import org.springframework.stereotype.Repository;
public interface Userdao {
String getUserName();
}
实现类:
package com.huahua.dao.Impl;
import com.huahua.dao.Userdao;
import com.huahua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
//dao层的注解 和@Component一样
@Repository(value = "userdao")
public class UsedaoImpl implements Userdao {
@Autowired
private User user;
public void setUser(User user) {
this.user = user;
}
@Override
public String getUserName() {
return user.getName();
}
}
4.service层:
接口:
package com.huahua.Service;
public interface UserService {
void getUserName();
}
package com.huahua.Service.Impl;
import com.huahua.Service.UserService;
import com.huahua.dao.Impl.UsedaoImpl;
import javafx.scene.chart.ValueAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
//service层的注释 同样和@compoent一样
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userdao")
private UsedaoImpl usedao;
public void setUsedao(UsedaoImpl usedao) {
this.usedao = usedao;
}
@Override
public void getUserName() {
System.out.println( usedao.getUserName());
}
}
5测试:
import com.huahua.Controller.ControllerDemo;
import com.huahua.Service.Impl.UserServiceImpl;
import com.huahua.dao.Userdao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl userService = context.getBean("userService", UserServiceImpl.class);
userService.getUserName();
}
}
运行结果
花花
.衍生的注解
@Component有几个衍生注解,我们在web开发中会按照mvc三层架构分层
1.dao层 (@Repository)
2.service层 (@Service)
3.controller层 (@Controller)
这个四个注解功能一样,将某个类注册到Spring容器中,装配!