Spring 基于注解的开发

时间:2021-04-22 20:34:38

Spring基于注解的开发



  注解就是一个类,使用@注解名称,用于取代xml配置文件。

  (一)普通注解:@Component("id") 取代 <beanid="" class=""> 【见例一】

  (二)web开发提供三个注解: 【见例二】

     (1)@Repository :dao层

     (2)@Service:service层

     (3)@Controller:web层

(三)依赖注入(给私有字段或者是setter方法设值)

      (1)普通值 @Value("")

      (2)引用值:

           按照类型@Autowired,

           按照名称@Autowired

                    @Qualifier("名称")

           按照名称 @Resource("名称")

   (四)生命周期  【见例三】

             初始化:@PostConstruct

             销毁:@PreDestroy                    

   (五)作用域:@Scope("prototype")多例

 

【注意】使用注解的前提,添加命名空间,让Spring扫描含有注解的类。

<beansxmlns="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:component-scanbase-package="?"></context:component-scan>

 

例一:使用@Component("id") 取代<bean id="" class="">

      步骤:编写User接口,编别UserImlp实现类,编写xml,编写测试类。

 

public interface User {
public void add();
}

@Component("UserImpleId")
public class UserImple implements User {
@Override
public void add() {
// TODO Auto-generated method stub
System.out.println("annotion addUser");
}

}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_a_component"></context:component-scan>
</beans>

public class Test {

@org.junit.Test
public void test() {
String xmlPath = "com/Lily/SpringLearning/f_annotation_a_component/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
User u = (User) applicationContext.getBean("UserImpleId");
u.add();
}

}


例二:web开发的三个注解

步骤:

1、编写my_service接口

2、编写 my_dao接口

3、编写my_daoImpl实现类,并注解@Repository("mydaoId")

4、编程my_serviceImpl实现类,并提供注解@Service,在调用dao的方法前面加上注解:@Autowired      @Qualifier("mydaoId ") 【按照名称注入】

5、编写action类,并提供注解@Controller("ActionId") 在调用service服务前 注解@Autowired

6、编写xml,扫描包。

7、编写测试类,这里getBean需要get的是action的Id

 

public interface my_Service {
void add();
}

public interface my_dao {
void save();
}

@Repository("mydaoid")
public class my_daoImpl implements my_dao {

/* (non-Javadoc)
* @see com.Lily.SpringLearning.f_annotation_b_web.my_dao#save()
*/
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("Dao save() work! ");
}

}

@Service
public class my_serviceImpl implements my_Service {

private my_dao dao;


@Autowired
@Qualifier("mydaoid")
public void setDao(my_dao dao) {
this.dao = dao;
}



@Override
public void add() {
dao.save();

}

}

@Controller("myactionId")
public class my_Aciton {

@Autowired
private my_Service s;
public void DoAction(){
s.add();

}
}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_b_web"></context:component-scan>

</beans>

public class Test {

@org.junit.Test
public void test() {
String xmlpath="com/Lily/SpringLearning/f_annotation_b_web/beans.xml";
ApplicationContext ap=new ClassPathXmlApplicationContext(xmlpath);
my_Aciton ac=(my_Aciton) ap.getBean("myactionId");
ac.DoAction();
}}


例三 生命周期:初始化和销毁

步骤:

1、写一个User接口

2、写UserImple实现类,并在类名前加上注解@Service("userId") ,然后在初始化类前面加上注解@PostConstruct,在销毁方法前加上注解 @PreDestroy

3、编写xml扫描组件

4、编写测试类,【注意】这里的测试类的getBean方法和以前的不太一样,要用类.class来获得!

 

public interface User {
public void add();
}

@Service("UserId")
public class UserImpl implements User {

/* (non-Javadoc)
* @see com.Lily.SpringLearning.f_annotation_c_lifecycle.User#add()
*/
@Override
public void add() {
System.out.println("LifeCycle User add() ");
}

@PostConstruct
public void myInit(){
System.out.println("init() !");
}

@PreDestroy
public void myDestory(){
System.out.println("destory() !");
}


}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_c_lifecycle"></context:component-scan>

</beans>

public class Test {

@org.junit.Test
public void test() {
String xmlPath = "com/Lily/SpringLearning/f_annotation_c_lifecycle/beans.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
User user = applicationContext.getBean("UserId" ,User.class);
User user2 = applicationContext.getBean("UserId" ,User.class);

System.out.println(user);
System.out.println(user2);

applicationContext.close();
}

}