My view on the Spring container Initiation (iv)

Source: Internet
Author: User

1. We often find annotations in our service and no annotations on DAO

Look at the picture




Because when we initialize the bean in the spring container, we treat the service as a bean, and DAO is not a bean, which is a personal understanding, if there are errors, you are welcome to point out.

2. Let's summarize some of the beans that are spring.

Look directly below the following four annotations are added to the class, which we all see as spring beans.

@Component, @Repository @Service, @Controller

Look at the literal meaning, easy but not out of three of them:

@Controller control layer, that's our action layer.

@Service The business logic layer, which is our service or manager layer

@Repository persistence layer, which is what we often call the DAO layer

and @component (which literally means a component) is used when you can't decide which layer to use.

In fact, these four annotations have the same effect, and spring will load them as a bean that needs to be injected into the context;

However, in the project, it is recommended that you use the project in strict accordance with the meanings of the remaining three annotations except Componen. This is good for a layered Web architecture!!

Example:

1. Control layer

@Controller//Comment as Controller

@RequestMapping ("/login")

public class Loginaction {

@Autowired

@Qualifier ("UserService")//Note Specify the injected Bean

Private Iuserservice UserService;

。。。。。。 Other slightly ...

}

2. Business Logic Layer

@Service ("UserService")

public class Userserviceimpl implements Iuserservice {

@Autowired

@Qualifier ("Userdao")

Private Iuserdao Userdao;

。。。。。。 Other slightly ...

}

3. Persistence Layer

@Repository ("Userdao")

public class Userdaoimpl implements Iuserdao {

private static Logger Logger = Loggerfactory.getlogger (Userdaoimpl.class);

Private DataSource DataSource;

private JdbcTemplate template;

@Autowired

Public Userdaoimpl (DataSource DataSource) {

This.datasource= DataSource;

Template = new JdbcTemplate (This.datasource);

}


3. The problem has come. Like our SPRINGMVC framework. In the persistence layer of DAO It is not implemented class. Because we write the DAO directly inherit Crudrepository<xx, xx>

At this time we write DAO is not counted bean it. I experimented.

Import Org.springframework.beans.factory.BeanFactory;

Tested in the controller.

@Autowired
Private Beanfactory beanfactory;

I have here. Bmworkdao is inherited crudrepository<xx, xx> no implementation class, because inheritance Crudrepository<xx, xx> also do not need to implement the class. A little more verbose.

String ss = "Org.szgzw.ent.compare.bmwork.BmWorkDAO";
Bmworkdao BB = (Bmworkdao) Beanfactory.getbean (Class.forName (ss));

System.out.println (Bb.getclass (). GetName ());

Anyway, there is no error, the specific Bmworkdao here is not a bean, I do not know, since no error, I think it is Spring container bean.


4. Let's add a little more to the 3rd.

We saw that we injected in the controller.

@Autowired
Private Beanfactory beanfactory;

We directly inject the bean factory and go to fetch the bean. In the usage scenario, the container is in the startup, and Tomcat is in the boot. Pay attention to this way of getting the bean.




My view on the Spring container Initiation (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.