SSH项目Class类的注解与属性的注解

  经过一段日子对SSH的学习,为了有利于随时能熟练的把一个SSH的项目快速的搭建起来,并且在报错的时候,将报错信息和解决

方案记录下来,每天一次的代码练习已成为家常便饭

  所以,在反复练习的时候,发现Spring的配置太繁琐。考虑开发大项目上,假设项目的action层会有上千个Class类,不简化代码的

情况下,就要在Spring的配置中装配上千个actionClass的bean,累抽。。

解决方案:

  在项目中加入注解,让Spring自动的为Class类定义的属性装配bean以及让Spring自动的扫描程序包中的类,隐式的在配置文件中

添加Class类的bean。

  注解分为两种,一是类的注解,二是类中属性的注解

  注解功能的提供也有两者,一是Spring,二是Jdk    (PS:类的注解只有springframework提供的

在后面--定义Bean的注解Spring自动扫描器 的声明标签中package的指定,梦逸`会详细的把细节罗列,有附图

---------常用注解--------

--自动装配Bean (选用一种注解就可以)

{ 作用:为Class类中定义的属性注入Class类实例 }

PS:SpringXml中一个bean就是一个Class类的实例

在SpringXml里面配置注解解析器,Spring会自动的给Class类中定义的属性装配bean

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:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

     <!-- 配置注解解析器 -->
    <context:annotation-config />

</beans>

简化:{配置好后,Class类中属性的setter方法可以删除,SpringXml中Bean_property也可以同时删除} 

@Autowired  (Srping框架自带的)

默认按类型匹配,自动装配,可以写在成员属性上,或写在setter方法上

@Autowired(required=true)  

一定要找到匹配的Bean,否则抛异常。 默认值就是true 

Java代码:

public class BcDaoImpl implements BcDao {
	
	@Autowired
	private SessionFactory sf;

} 

@Autowired

@Qualifier("bean的名字") 

按名称装配Bean,与@Autowired组合使用,解决Spring按类型匹配找到多个Bean并给之装配的问题。

Java代码:

public class BcDaoImpl implements BcDao {
	
	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sf;

}

  

@Resource  (Jdk提供的)

默认按名称装配,当找不到名称匹配的bean再按类型装配.

将注解声明在成员属性的上方

可以通过@Resource(name="beanName") 指定要被注入的bean的名称, 要是未指定name属性, 默认使用成员属性的变量名,一般不用写name属性.

@Resource(name="beanName")指定了name属性,按名称注入但没找到bean, 就不会再按类型装配了.

Java代码:

public class BcDaoImpl implements BcDao {
	
	@Resource(name="sessionFactory")
	private SessionFactory sf;
	
}

  

@Inject   (Jdk提供的)

按类型装配,功能比@Autowired少,没有使用的必要。

Java代码:

public class BcDaoImpl implements BcDao {
	
	@Inject()
	private SessionFactory sf;
	
}

--定义Bean的注解

在SpringXml中配置自动扫描器

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:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    <!-- 配置程序包自动扫描器   -->
    <context:component-scan base-package="app" />
    
    <!-- 
        使用自动扫描包的情况下
        base-package属性的值就是一个包名
        前提是action(Controller)包 、service包、dao包3个子包必须都在同一个主包里面
        
        下面梦逸附一张图给大家看看^_^
     -->

</beans>

简化:{配置好后,Class对应在Spring中的bean就可以都删除掉,Spring会隐式的添加Class类的bean,所以就看不到代码,有没有感觉清爽多了?   ^_^ }  

PS:使用了<context:component-scan base-package="" />标签后,就可以不需要<context:annotation-config  />标签了,因为自动扫描器标签除了会隐式的添加bean,同时也会自动的给Class类中的

属性装配bean

附图:

(PS:若想该bean每次被调用都创建出一个实例,必须在bean注解的后面使用注解@Scope("")指定作用域)

@Controller

@Controller("bean的Id属性值,即Bean的名称")

定义控制层的Bean,如Action

Java代码:

@Controller
@Scope("prototype")
public class BcAction extends ActionSupport {
	@Autowired
	private BcService bs;
	
	private List<BookCard> bcList = new ArrayList<BookCard>();
	public List<BookCard> getBcList() {
		return bcList;
	}
}

  

 

@Service          

@Service("bean的Id属性值,即Bean的名称")

定义业务层的Bean

Java代码:

@Service
@Scope("prototype")
public class BcServiceImpl implements BcService {
	@Autowired
	private BcDao bd;
	
	@Override
	public List<BookCard> getAllBc() {
		// TODO Auto-generated method stub
		return bd.getAllBc();
	}
}

  

 

@Repository   

@Repository("bean的Id属性值,即Bean的名称")

定义DAO{数据库数据表的操作}层的Bean

Java代码:

@Repository
@Scope("prototype")
public class BcDaoImpl implements BcDao {
	
	@Autowired
	private SessionFactory sf;

}

  

 

 @Component  

定义Bean, 不好归类时使用.

Java代码:

@Component("bcDao") //可以在小括号中指定bean的id名称,不指定就写@Component
public class BcDaoImpl implements BcDao {
	
	@Autowired
	private SessionFactory sf;
	
}

  

  

-----------------------------------------------------------------------------

(完)                梦逸、原创,转载请注明原文地址

  

  

原文地址:https://www.cnblogs.com/LiJinfu/p/5960618.html