| |

VerySource

 Forgot password?
 Register
Search
View: 787|Reply: 3

How to use the embedded java code to get the beans defined in the spring configuration file in the JSP page?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-13 11:00:02
| Show all posts |Read mode
For example, a test.jsp page, the user directly requests the page,
Need to get spring's applicationContext.xml in this page
Defined bean, how to do it?
You cannot use the new ClassPathXmlApplicationContext () method in the page, as this will cause
The bean has been initialized repeatedly. Thank you in advance!
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-8 21:15:01
| Show all posts
context = (ApplicationContext) getServletContext().getAttribute(
                ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
        logger.info("The count of definition Bean in the application is :"
                + context.getBeanDefinitionCount());
        //Get the data source connected to the sybase database
        //DataSource ds = (DataSource) context.getBean("applicationds1");
        //Store the obtained sybase data source in a static variable
        //LoadCodesServlet.sybaseDS = ds;
        //Get the injected session factory class and get a connection to the database through the factory
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");


So you can get it.
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-6-9 10:39:40
| Show all posts
Positive solution upstairs.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-10 13:00:01
| Show all posts
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

            InitParamService p = (InitParamService)ctx.getBean("initParamService");
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list