spring boot 添加扫包 集成 jpa

hibernate实体类:

@Entity
@Table(name = "tb_uc_account")
public class Account {

	@Id
	@GeneratedValue
	@Column(nullable = false, name = "ucac_fl_id")
	private long flId;
	@Column(nullable = false, name = "ucac_fl_name")
	private String username;
	@Column(nullable = false, name = "ucac_fl_password")
	private String password;
	@Column(nullable = false, name = "ucac_regist_time")
	private Date registDate;
	@Column(nullable = false, name = "ucac_status")
	private int status;

	/**
	 * 账号绑定信息
	 */
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
	@JoinColumn(name ="uab_fl_id")
	private List bindInfos;

	
	/**
	 * 游戏账号信息
	 */
	@OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "aabi_fl_id")
	// @JoinColumn(name = "uab_user_id", referencedColumnName = "ucac_user_id")
	private List appBindInfos;
	
	/**
	 * 一个用户玩过的游戏信息:
	 */
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "uag_user_id")
	private List games;

	public Account() {
	}

	public Account(String username, String password, int status) {
		super();
		this.username = username;
		this.password = password;
		this.registDate = new Date(System.currentTimeMillis());
		this.status = status;
	}
	
	public long getFlId() {
		return flId;
	}

	public void setFlId(long flId) {
		this.flId = flId;
	}
。。。。。

@Table@Entity是hibernate-jpa的注解,那么spring boot的容器如何管理这些bean呢?如何识别呢?????


数据源配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.feiliu.monkey.open.repo.primary" }) //设置Repository所在位置
public class PrimaryConfig {

    @Autowired @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                .packages("com.open.domain.primary") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }


通过配置类可以看到:通过EntityManagerFactoryBuilder类来添加需要注入的bean;  函数 .packages(“。。。”)里 是需要扫描的bean的包路径。和spring mvc 添加扫包路径一个意思。

那么可以猜测,spring容器一定对hibernate的注解实现了自己的处理,来注入这些bean。

参考:https://www.cnblogs.com/guscode/p/5518464.html


你可能感兴趣的:(spring)