1.看代码,下面是一个类
下面主要红色字体部分的属性
package
import java.util.List;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.westsoft.bean.Person;
import com.westsoft.dao.PersonDao;
import com.westsoft.service.PersonService;
@Service @Transactional
public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate;
@Resource
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Resource
private PersonDao personDao;
/*
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}*/
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
}
@Override
public Person getPerson(Integer id) {
// TODO Auto-generated method stub
return null;
}
@Override @Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<Person> getPersons() {
// TODO Auto-generated method stub
return null;
}
/*
* 新增
* @Transactional(rollbackFor=Exception.class)
* RuntimeException运行期例外,默认回滚
* Exception 非运行期例外,不回滚
* @Transactional(rollbackFor=Exception.class)
* @Transactional(propagation=Propagation.NOT_SUPPORTED):不开启事务
* @Transactional(propagation=Propagation.REQUIRED):默认事务行为,如果方法运行时已经处在一个事务中,则加入该事务,
* 否则为自己创建一个事务
* @Transactional(propagation=Propagation.REQUIREDNEW):必须开启一个新事务,事务中嵌套事务
* @Transactional(propagation=Propagation.SUPPORTS):如果方法在已存在的事务中执行,则加入该事务,如果不在事务中执行,
* 则方法在没有事务的环境下执行.
* @Transactional(propagation=Propagation.MANDATORY):指定方法只能在已有的事务中执行,不会自动创建事务,如果没有在事务
* 中执行,则抛出异常.
* @Transactional(propagation=Propagation.NEVER):不能在事务中执行,否则抛出异常
*
* @Transactional(propagation=Propagation.NESTED):
*
*/
@Override @Transactional(propagation=Propagation.REQUIRED)
public void save(String name) throws Exception {
// TODO Auto-generated method stub
this.jdbcTemplate.update("insert into person(name) values(?)", new Object[]{name},
new int[]{java.sql.Types.VARCHAR});
//throw new RuntimeException("运行期例外");
}
@Override
public void update(Person person) {
// TODO Auto-generated method stub
}
}
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
2.单元测试代码
class).getPropertyDescriptors();
for(PropertyDescriptor properdesc : ps){
System.out.println(properdesc.getName()+":"+properdesc.getPropertyType());
}
3.测试输出
class:class java.lang.Class
jdbcTemplate:interface javax.sql.DataSource
persons:interface
jdbcTemplate,属性的数类型是javax.sql.DataSource
由此可见类的属性的类型并不是由属性所关联的字段的类型决定的,而是由传入的参数的类型决定的
4.如果有getAaa则属性的类型由getAaa的返回值决定,如下代码
package
import java.util.List;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.westsoft.bean.Person;
import com.westsoft.dao.PersonDao;
import com.westsoft.service.PersonService;
@Service @Transactional
public class PersonServiceBean implements PersonService {
private String bbb;
public Integer getAaa() {
return 0;
}
public void setAaa(Integer aaa) {
this.bbb = (String)aaa.toString();
}
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate ;
}
@Resource
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Resource
private PersonDao personDao;
/*
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}*/
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
}
@Override
public Person getPerson(Integer id) {
// TODO Auto-generated method stub
return null;
}
@Override @Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<Person> getPersons() {
// TODO Auto-generated method stub
return null;
}
/*
* 新增
* @Transactional(rollbackFor=Exception.class)
* RuntimeException运行期例外,默认回滚
* Exception 非运行期例外,不回滚
* @Transactional(rollbackFor=Exception.class)
* @Transactional(propagation=Propagation.NOT_SUPPORTED):不开启事务
* @Transactional(propagation=Propagation.REQUIRED):默认事务行为,如果方法运行时已经处在一个事务中,则加入该事务,
* 否则为自己创建一个事务
* @Transactional(propagation=Propagation.REQUIREDNEW):必须开启一个新事务,事务中嵌套事务
* @Transactional(propagation=Propagation.SUPPORTS):如果方法在已存在的事务中执行,则加入该事务,如果不在事务中执行,
* 则方法在没有事务的环境下执行.
* @Transactional(propagation=Propagation.MANDATORY):指定方法只能在已有的事务中执行,不会自动创建事务,如果没有在事务
* 中执行,则抛出异常.
* @Transactional(propagation=Propagation.NEVER):不能在事务中执行,否则抛出异常
*
* @Transactional(propagation=Propagation.NESTED):
*
*/
@Override @Transactional(propagation=Propagation.REQUIRED)
public void save(String name) throws Exception {
// TODO Auto-generated method stub
this.jdbcTemplate.update("insert into person(name) values(?)", new Object[]{name},
new int[]{java.sql.Types.VARCHAR});
//throw new RuntimeException("运行期例外");
}
@Override
public void update(Person person) {
// TODO Auto-generated method stub
}
}
5.测试输出
注意红色部分
public Integer getAaa() {
return 0;
}publicvoid setAaa(Integer aaa) {
this.bbb = (String)aaa.toString();
}
通过反射测试出属性的数据类型为
class
class:class java.lang.Class
jdbcTemplate:class org.springframework.jdbc.core.JdbcTemplate
persons:interface
6.总结
在没有getAaa的情况下,属性的数据类型是有setAaa的传入参数决定,如果有getAaa则属性的数据类型有getAaa的返回类型决定.