Spring系统学习--2注解汇总

@Component:

/**
 * 原来在XML的配置
 * <bean id="IAccountService_annotation" class="main.java.service.impl.IAccountService_annotationImpl" >
 *     <property name="iAccountDao_annotation" ref="dao"></property>
 * </bean>
 * 上面原来在XML里面的配置现在可以使用下面的注解搞定:
**/
/** 
@Component:
 *          作用:就相当于在spring的xml配置文件中写了一个bean标签。
 *          属性:
 *              value:用于指定bean的id。当不写时,默认值是当前类名,首字母改小写。例如:accountServiceImpl
 *      由此注解衍生的三个注解:
 *                @Controller:一般用于表现层
 *                @Service:一般用于业务层
 *                @Repository:一般用于持久层
 *                他们的作用以及属性和@Component的作用是一模一样的。他们的出现是spring框架为我们提供更明确的语义化来指定不同层的bean对象。
*/
//@Component(value = "IAccountService_annotation")
//@Service(value = "IAccountService_annotation")
@Repository(value = "IAccountService_annotation")
public class IAccountService_annotationImpl implements IAccountService_annotation {
...
}

@Autowired,@Qualifier, @Resource

/**
        @Autowired
 *           作用:自动按照类型注入。只要容器中有唯一的类型匹配,则可以直接注入成功。如果没有匹配的类型就报错。
 *                如果有多个类型匹配时,会先按照类型找到符合条件的对象,然后再用变量名称作为bean的id,从里面继续查找,如果找到仍然可以注入成功,如果没有匹配的id,就报错
 *           细节:当使用此注解注入时,set方法就可以省略了。
 *           属性:
 *               required:是否必须注入成功。取值是true(默认值)/false。当取值是true时,没有匹配的对象就报错。
 *           @Qualifier作用:在自动按照类型(@Autowired)注入的基础之上,再按照bean的id注入。在给类成员注入时,它不能够浊立使用。
 *           属性:
 *              value:用于指定bean的id。
 *           @Resource作用:直接按照bean的id注入。
 *           属性:
 *              name:用于指定bean的id。
 *          以上3个注解,都只能用于注入其他bean类型,而不能注入基本类型和String。
**/
@Component(value = "IAccountService_annotation")
public class IAccountService_annotationImpl implements IAccountService_annotation {
  //@Autowired
  //@Qualifier(value = "dao")
    @Resource(name = "dao")
    private IAccountDao_annotation iAccountDao_annotation;

 /*   public void setiAccountDao_annotation(IAccountDao_annotation iAccountDao_annotation) {
        this.iAccountDao_annotation = iAccountDao_annotation;
    }*/

    @Override
    public void saveAccount() {
        iAccountDao_annotation.save();
    }
}

@Value:

/**
           @Value作用:用于注入基本类型和String类型的数据。
           属性:
                value:用于指定要注入的数据。它支持使用spring的el表达式。
                spring的el表达式写法:
                                    ${表达式}
**/

@Repository(value = "IAccountService_annotation")
public class IAccountService_annotationImpl implements IAccountService_annotation {
    @Value("https://www.hao123.com/")
    public String url;
    @Value("${jdbc.driver}")
    private String driver;
 @Override
    public void saveAccount() {
        System.out.println(url);//https://www.hao123.com/
        System.out.println(driver);//com.mysql.jdbc.Driver;
        iAccountDao_annotation.save();
    }
}
spring的el表达式的使用前提是:
1.我们要提前创建了相应的配置文件:

 2.我们在spring的xml配置文件里面添加如下注解:

<!--    告知Spring,properties配置文件的位置-->
    <context:property-placeholder location="dataBaseConfig.properties"></context:property-placeholder>

@Scope

/**
       三,用于改变作用范围:
 *      @Scope
 *          作用:用于改变bean的作用范围。范围的取值和xml中的配置是一样的(单例:singleton;多例:prototype)
 *          属性:
 *              value:用于指定范围。
**/
@Component(value = "IAccountService_annotation")
@Scope("prototype")//多例(默认情况是单例)
public class IAccountService_annotationImpl implements IAccountService_annotation {
...
}

测试:

public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean_annotation.xml");
        IAccountService_annotation annotation = context.getBean("IAccountService_annotation", IAccountService_annotation.class);
        IAccountService_annotation annotation2 = context.getBean("IAccountService_annotation", IAccountService_annotation.class);
        System.out.println(annotation==annotation2);//false
}

@PostContruct,@PreDestroy

/**
 *      四,和生命周期相关的:
 *          @PostContruct
 *              作用:用于指定初始化方法。和配置文件中init-method属性是一样的
 *          @PreDestroy
 *              作用:用于指定销毁方法。和配置文件中destroy-method属性是一样的
 */
@Component(value = "IAccountService_annotation")
@Scope("singleton")
public class IAccountService_annotationImpl implements IAccountService_annotation {
    @PostConstruct
    public void init(){
        System.out.println("对象初始化了");
    }
    @PreDestroy
    public void destory(){
        System.out.println("对象销毁了");
    }
}

@Configuration

@Configuration//它就相当于表明当前类是spring的配置类,如果只是写到AnnotationConfigApplicationContext构造函数中的字节码,可以不写。//如果是加载要扫描的包时,需要读到此类的配置,同时又没把此类的字节码提供给AnnotafionConfigApplicationContext构造函数,则必须写。
@ComponentScan("com.dao")//指定创建容器时要扫描的包
public class Springconfiguration{
...
}

使用上面注解配置的配置类,我们就不需要spring的xml配置文件了,那么我们怎么获取spring的容器呢?

private ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfiguration. class);//可以接受多个配置类的class字节码对象

@Import

@ComponentScan("com.all")//指定创建容器时要扫描的包
@Import(JdbcConfig.class)//用于导入其他的配置类
public class Springconfiguration{
...
}

@Bean

/**
*@Bean注解:
*作用:把当前方法的返回值作为bean对象存入spring容器之中。
*属性:
*name:用于指定bean的id。如果没该属性的话,默认值是当前的方法名。

*Spring框架给带有bean注解的方法创建对象时,如果方法有参数,会用方法参数的数据类型前往容器中查找。

*如果能找到唯一的一个类型匹配,则直接给方法的参数注入。

*如果找不到,就报错

*如果找到多个,就需要借助@Qualifier注解,此时它可以独立使用
*/
/**只有一个同类(DataSource )bean对象的情况下
@Bean(name="dbAssit'")
public DBAssit createDBAssit(DataSource dataSource){
return new DBAssit(dataSource);
}
**/
/**多个同类(DataSource )bean对象的情况下**/
@Bean(name="dbAssit")
public DBAssit createDBAssit(@Qualifier("ds1)Datasource dataSource){
return new DBAssit(dataSource);
}
/**
*创建数据源
*/
@Bean(name="ds1")
public DataSource createDataSource1(){
try{
ComboPooledDataSource ds=new ComboPooledDataSource();
ds.setDriverClass("com.mysq1.jdbc.Driver");
ds.set]dbcUr1("jdbc:mysq1://localhost:3306/spring5");
ds.setUser("root");
ds.setPassword("1234");
return ds;
 }catch(Exception e){
throw new RuntimeException(e);
             }
}
@Bean(name="ds2")
public DataSource createDataSource1(){
try{
ComboPooledDataSource ds=new ComboPooledDataSource();
...
return ds;
 }catch(Exception e){
throw new RuntimeException(e);
             }
}

@PropertySource

|@ComponentScan("com.all")//指定创建容器时要扫描的包
@Import(JdbcConfig.class)/用于导入其他的配置类
@PropertySource("classpath:config/jdbcConfig.properties")//配置要添加到spring容器的配置文件的位置
public class Springconfiguration{
}
public class JdbcConfig {
@ Value("${ jdbc. driver}")
private String driver;
@ Value("${ jdbc. url}")
private String url;
@ Value("${ jdbc. username}")
private String username;
@ Value("${jdbc. pas sword}")
private String password;
}

@Transactional

//账户业务层实现类
@Service("accountService")
/**
 * @Transactional可以出现在接口上,类上和方法
 * 接口上:表示该接口中的所有方法,实现类中重写的方法都有事务
 * 类上:表示该类中所有的方法都有事务
 * 方法上:表示当前方法有事务
 * 优先级:就近原则。
 * 方法>类>接口
 */
@Transactional(readOnly = false)
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Override
    public void transfer(String sourseName, String targetName, Float money) {
        //        根据账户名查询对应的账户
        Account sourceAccount = accountDao.findAccountByName(sourseName);
        Account targetAccount = accountDao.findAccountByName(targetName);
//        转出账户减钱,转入账户加钱
        sourceAccount.setMoney(sourceAccount.getMoney() - money);
        targetAccount.setMoney(targetAccount.getMoney() + money);
//        更新两个账户
        accountDao.updateAccount(sourceAccount);
//        模拟异常
        int i = 1 / 0;
        accountDao.updateAccount(targetAccount);

    }

    @Override
    public Account findAccountById(Integer id) {
        return accountDao.findAccountById(id);
    }
}

@EnableTransactionManagement

开启spring对注解事务的的支持

@Configuration
@EnableTransactionManagement
public class SpringTxConfiguration {
    //里面配置数据源,配置JdbcTemplate,配置事务管理器。在之前的步骤已经写过了。
}



原文地址:https://www.cnblogs.com/luzhanshi/p/13227873.html