1.注解介绍
(1)代码里面特殊标记,可以使用注解完成功能
(2)注解写法 @注解名称(属性名称=属性值)
(3)注解使用在类上面,方法上面,属性上面
2.注解准备工作
(1)导入基本jar包
(2)注解方式多加一个aop的jar包
(3)创建新建类
(4)配置spring中的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"> <!-- 注解引入约束 -->
</beans>
3.注解创建对象
(1)创建一个User类
package cn.istic.demo;
import org.springframework.stereotype.Component;
@Component(value="user") //等价于 <bean id="" class=""></bean>创建对象
public class User {
public void add(){
System.out.println("add.........");
}
}
(2)配置文件
<?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"> <!-- 注解引入约束 -->
<!-- 开启注解扫描
(1)到包里面扫描类,方法,属性上面是否有注解
-->
<context:component-scan base-package="cn.istic.demo"></context:component-scan>
</beans>
(3)测试方法
package cn.istic.demo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testzhujie {
@Test
public void test(){
//加载配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
//得到配置创建对象
User user= (User) context.getBean("user");
user.add();
}
}
4创建对象的注解
目前这四个注解的功能都是一样的,@Componen可以等价换成任何一个
5.注解注入属性
要求创建Service类,创建Dao类,在Service类中得到Dao对象
(1)注入属性第一个注解@Autowried自动注入和注入对象无关,根据类名来找
Service类
package cn.istic.demo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//value可以省略不写
@Service(value="userservice")
public class UserService {
//1.得到DAO类型的属性
//在dao属性上面使用注解,完成对象注入
@Autowired
private UserDao userdao;
//2.注解方式不需要创建set方法
public void test(){
System.out.println("service......");
userdao.dao();
}
}
Dao类
package cn.istic.demo2;
import org.springframework.stereotype.Component;
//value可以省略不写
@Component(value="userdao")
public class UserDao {
public void dao(){
System.out.println("dao......");
}
}
配置文件
<?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"> <!-- 注解引入约束 -->
<!-- 开启注解扫描
(1)到包里面扫描类,方法,属性上面是否有注解,包名改变 base-package跟着改变
-->
<context:component-scan base-package="cn.istic"></context:component-scan>
</beans>
测试类
package cn.istic.demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testzhujie {
@Test
public void test(){
//加载配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
//得到配置创建对象
UserService user= (UserService) context.getBean("userservice");
user.test();
}
}
(2)注入属性第二个注解@Resource(name="")
Service类
package cn.istic.demo2;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//value可以省略不写
@Service(value="userservice")
public class UserService {
//1.得到DAO类型的属性
//在dao属性上面使用注解,完成对象注入
//@Autowired
//@Resource中的name属性为写注解创建Dao的value值
@Resource(name="userdao")
private UserDao userdao;
//2.注解方式不需要创建set方法
public void test(){
System.out.println("service......");
userdao.dao();
}
}
dao类
package cn.istic.demo2;
import org.springframework.stereotype.Component;
//value可以省略不写
@Component(value="userdao")
public class UserDao {
public void dao(){
System.out.println("dao......");
}
}
测试类
package cn.istic.demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testzhujie {
@Test
public void test(){
//加载配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
//得到配置创建对象
UserService user= (UserService) context.getBean("userservice");
user.test();
}
}
6.注解和配置文件混合使用
(1)创建对象使用配置文件方式
(2)注入属性方式使用注解
Service类
package cn.istic.demo2;
import javax.annotation.Resource;
public class UserService {
@Resource(name="userdao")
private UserDao userdao;
//2.注解方式不需要创建set方法
public void test(){
System.out.println("service......");
userdao.dao();
}
}
Dao类
package cn.istic.demo2;
public class UserDao {
public void dao(){
System.out.println("dao......");
}
}
配置文件
<?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"> <!-- 注解引入约束 -->
<!-- 开启注解扫描
(1)到包里面扫描类,方法,属性上面是否有注解
-->
<context:component-scan base-package="cn.istic"></context:component-scan>
<!-- 创建Service对象 -->
<bean id="userservice" class="cn.istic.demo2.UserService"></bean>
<!-- 创建userDAo对象 -->
<bean id="userdao" class="cn.istic.demo2.UserDao"></bean>
</beans>
测试类
package cn.istic.demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testzhujie {
@Test
public void test(){
//加载配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
//得到配置创建对象
UserService user= (UserService) context.getBean("userservice");
user.test();
}
}